Filter phone numbers out of message - php

I'd like to filter phone numbers out of the users message. The problem is ofcourse that a phone number can be written in different ways. Like:
0612345678
06 123 45 678
+31 (0)612345678
+31 (0)6 12 34 56 78
But I've got absolutely no clue how to do this and I'm pritty stuck. Can anyone help me a bit?
Thanks!
Edit:
In the meanwhile I came with this regular expression: "/(\d|\s){5,}/im". This filters every number of at least 5 characters and ignores the spaces. That way, all numbers from my previous example will be filtered.

You need to use regular expressions. This link pertains to python, but you can use these patterns by calling the PHP functions: http://diveintopython3.ep.io/regular-expressions.html#phonenumbers
http://php.net/manual/en/book.pcre.php

Related

How to prevent contact detail exchange?

Is there any algorithm or library (e.g. in PHP) that provides a "phone number exchange prevention"?
Basically e.g. a phone number like
0123 45 67 89
can easily be removed by regular expression.
But a number like
0
1
2
3
4
5
can harder be detected. And then even harder, the hardest case:
my number is: zero one two three four five six seven
How would you remove something like this via PHP regex? Is there a library?

Regular Expression PHP Phone Number

I am making a regular expression that will read phone numbers from a PHP form. I have the expression most of the way completed. It needs to read a phone number in any of the following formats:
623-456-7890
456-7890
6234567890
4567890
623.456.7890
456.7890
623 456 7890
456 7890
The expression I have made at this point is the following:
(([0-9]{3}){0,1})((\W){0,1})([0-9]{3})((\W){0,1})([0-9]{4})
It mostly works, the only phone number it doesn't read is the third one in the above list (6234567890). What would I have to add or change to make it read that phone number?
That's much better:
([0-9]{3})?\W*([0-9]{3})\W*([0-9]{4})
Real phone numbers are much more complicated than this because of exchanges and so forth. This will match numbers, periods and hyphens, not all are required, but it also doesn't check for valid phone numbers.
([0-9]{3})?[ .-]?([0-9]{3})[ .-]?([0-9]{4})
/(?:[\(]?\d{3}[\)\.\- ]?)?\d{3}[\.\- ]?\d{4}/
Here it is in practice: http://regex101.com/r/pL3dB0/3

restricting numbers of digit length in regex

Here is my regex to validate a phone number.
((^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{5}\)?[\s-]?\d{4,5}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$)|(\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}))|(?:\((\+?\d+)?\)|(\+\d{0,3}))? ?\d{2,3}([-\.]?\d{2,3} ?){3,4}
Here is the link for regex check http://regex101.com/r/xO4aU4
it validates UK US numbers. lower bound of Range of the number is 7 and higher bound is not restricted.
can I restrict it so that if range of the number is if less then 7 or greater then 14 then it should not filter the number at all.
(\+44)?\s?\(?0?\d{1,5}\)?\s\d{1,7}\s{0,1}\d{0,6}(?:\s-\s|\s)\s{0,2}\d{0,6}|(\+44)?\s?\(?\d{1,5}\)?\s\d{1,7}\s{0,1}\d{0,4}\s{0,1}\d{0,4}|(\+44)?\s?\(\d{1,5}\)\s?\d{3,7}\s?\d{0,4}\s?\d{0,4}|\d{4,5}\s*\d{3,5}\s\d{3,4}
That is a regex I use for Uk phone numbers (landlines) <- it is used in screen scraping sites so it is probably a little more robust and matches edge cases (such as people who put +44(0)1772 99 33 66) - it is used couple with string length checks and doesn't account for extension numbers - but you should put extension numbers as seperate field anyway.
I have no idea about US numbers so sorry can't help there!

Parsing input from user in any order or format

I am having some trouble trying to figure out how to parse information collected from user. The information I am collecting is:
Age
Sex
Zip Code
Following are some examples of how I may receive this from users:
30 Male 90250
30/M/90250
30 M 90250
M 30 90250
30-M-90250
90250,M,30
I started off with explode function but I was left with a huge list of if else statements to try to see how the user separated the information (was it space or comma or slash or hypen)
Any feedback is appreciated.
Thanks
It's easy enough. The ZIP code is always 5 digits, so a simple regex matching /\d{5}/ will work just fine. The Age is a number from 1 to 3 digits, so /\d{1,3}/ takes care of that. As for the gender, you could just look for an f for female and if there isn't one assume male.
With all that said, what's wrong with separate input fields?
You might want to use a few regular expressions:
One that looks for 5 numeric digits: [^\d]\d{5}[^\d]
One that looks for 2 numeric digits: [^\d]\d{2}[^\d]
One that looks for a single letter: [a-zA-Z]
[EDIT]
I've edited the RegExes. They now match every one of the presented alternatives, and don't require any alteration of the input string (which makes it a more efficient choice). They can also be run in any order.

Loose telephone regex validation

I've been trying to find a validation regex to do some loose validation on phone numbers. I'm going to strip some of the stuff out when I use them, but I would like to allow the user the freedom to enter their number as they want, and I want to display it as they have entered it.
I figured that the best thing to do is whitelist my characters. I figured on
[space] + - [0-9] ( )
Are there any other characters that I should be allowing? I'm not quite sure if I should be looking for characters which do not match this in the pattern?
So far all I can come up with is,
[\+0-9\s\-\(\)]
Which seems to match every character
I've been playing around in here, http://gskinner.com/RegExr/
Using this as my data,
+44 787 553 7794
+1-818-923-4821
&9_981-432 p
+44 (0) 20 874 1932
If anyone could nudge me in the right direction, I'd really appreciate it! Thanks :)
^[\+0-9\s\-\(\)]+$
^ and $ Will ensure we are matching the whole string
the + (before the final $) will allow the range to match the whole number (multiple characters)
What about keeping the string as the user enters it and then removing everything that is not a number or # or + if you want to dial it.
Using this as my data,
+44 (0) 20 8874 1932
When faced with a non-valid format like +44 (0) 20 8874 1932 simply removing the brackets leads to the non-valid number +44 020 8874 1932.
This number should be presented as +44 20 8874 1932 for international use and as (020) 8874 1932 for national use.
The international format does not include brackets.

Categories