Regular Expression PHP Phone Number - php

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

Related

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!

Is there any way to find country code from phone number In php?

I want to find country code from the phone number because phone number is without country code(which is my input) for instance my phone no is 9xxxxxxxxx and I get result that number is from India and it will display then +919xxxxxxxxx in php.Is there any way to find country code from phone number?
First you'd need a source for the data listing every country, its code and the number of digits and pattern of the number. Then you'd need to put that into an associative array, matching up each country with it's country code. Then you'd need a bunch of preg_matches using regular expressions to determine if the first digits plus the regular expression matched the country in the array. You've talking a huge project to program this from scratch.
You can build your own PHP function for a lookup like this, a list of all the Phone Netnumbers can be found on Wikipedia. If there is no net number it's not possible to track the country since countries can use the same phone numbers.

How to validate if Textfield entered is a mobile number using php and SQL DB [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to validate phone number using PHP?
can anyone please help me know how to validate if the field value entered is a phone number using php...
I have used a variable $phone , datatype =varchar 10 in sql db
Now i want to validate that users enter only numbers in that field..
use preg_match
if(preg_match('/^(NA|[0-9+-]+)$/',$str)) {
*code here*
} else {
"code here"
}
One way to do this is with regular expressions. When validating phone numbers, it's easier on users if you accept accompanying characters, and filter them out yourself (-+()).
http://www.php.net/manual/en/function.preg-replace.php
$phone = preg_replace ( '/[+\\.\\(\\) ]/' , '' , $phone);
Once you've done that, checking for a match of 10 digits (assuming U.S. numbers with area code) can be done like so:
if(preg_match ( '/^\\d{10}$/', $phone) ) {
// Good match
}
http://www.php.net/manual/en/function.preg-match.php
Does is_numeric solve your problem?
Edit:
I wasn't aiming to solve OPs problem, merely hoping to give him/her pointers. However, reading the question closer makes me think that OP isn't being conscious of internationalisation issues. Her field is 10 characters long, so a number like +447970122467 (a valid British mobile number) would cause a failure. I'm going to assume they are in North America, and as such can assume that all numbers are in accordance with the North American Numbering Plan. The description of this, in words, is taken from that page:
Component Name Number ranges Notes
+1 ITU country calling code "1" is also the usual trunk code for accessing long-distance service between NANP numbers. In an intra-NANP context, numbers are usually written without the leading "+"
NPA Numbering Plan Area Code Allowed ranges: [2–9] for the first digit, and [0–9] for both the second and third digits. Covers Canada, the United States, parts of the Caribbean Sea, and some Atlantic and Pacific islands. The area code is often enclosed in parentheses.
NXX Central Office (exchange) code Allowed ranges: [2–9] for the first digit, and [0–9] for both the second and third digits. Often considered part of a subscriber number. The three-digit Central Office codes are assigned to a specific CO serving its customers, but may be physically dispersed by redirection, or forwarding to mobile operators and other services.
xxxx Subscriber Number [0–9] for each of the four digits. This unique four-digit number is the subscriber number or station code.`
That ought to be enough to get OP started on solving their problem. Sorry for being curt in my initial response.

PHP - How should GPS LOCATION Regular expression look like?

I'm gonna have to learn to use regular expressions soon, but now I just need to know how should a check for "50.080215,14.393983" GPS format look like, thanks, Mike.
You want to find two decimal numbers separated by a comma (and maybe whitespace?) in a string?
$pattern = "/(?P<lat>(\d+(\.\d+)?)),\s*(?P<lon>(\d+(\.\d+)?))/";
This assumes that the fractional portion of each number may not be present if not needed and places no constraints on the number of digits of precision. Depending on your input corpus this may match more often than you want. With a better specification a tighter pattern could be constructed. For example if we specify that latitude will run form -90 to 90 inclusive and longitude will run from -180 to 180 inclusive and both may have up to 6 digits of precision we can construct this pattern:
$pattern = "/(?P<lat>-?([1-8]?[1-9]|[1-9]0)(\.\d{1,6})?)(?P<lon>-?(1?[1-7][1-9]|1?[1-8]0|[1-9]?[0-9])(\.\d{1,6})?)/";
There is a slight bug in this specification in that it will match "90.999999,180.999999" which is outside the hypothetical spec. Correcting this left as an exercise for the reader.

Filter phone numbers out of message

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

Categories