restricting numbers of digit length in regex - php

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!

Related

How can i check if number is matching a specific criteria with regexp in php

I need to validate a phone number and make sure it is starting with a specific 4 digits (2010,2011,2012) plus more 7 digits by using regular expressions in php.
This will do the job
^201(0|1|2){1}(\d){7}$

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

Generate a pseudo random 6 character string from an integer

I am trying to resolve the following problem via PHP. The aim is to generate a unique 6-character string based on an integer seed and containing a predefined range of characters. The second requirement is that the string must appear random (so if code 1 were 100000, it is not acceptable for code 2 to be 100001, and 3 100002)
The range of characters is:
Uppercase A-Z excluding: B, I, O, S and Z
0-9 excluding: 0, 1, 2, 5, 8
So that would be a total of 26 characters if I am not mistaken. My first idea would to be encoding from base 10 to base 24 starting at number 7962624. So do 7962624 + seed, and then base24 encode that number.
This gives me the characters 0-N. If I replace the resulting string in the following fashion, I then meet the first criteria:
B=P, I=Q, 0=R, 1=T, 2=U, 5=V, 8=W
So at this point, my codes will look something like this:
1=TRRRR, 2=TRRRT, 3=TRRRU
So my question to you gurus is: How can I make a method that behaves consistently (so the return string for a given integer is always the same) and meets the 2 requirements above? I have spent 2 full days on this now and short of dumping 700,000,000 codes into a database and retrieving them randomly I'm all out of ideas.
Stephen
You get a reasonably random looking sequence if you take your input sequence 1,2,3... and apply a linear map modulo a prime number. The number of unique codes is limited to the prime number so you should choose a large one. The resulting codes will be unique as long as you choose a multiplier that's not divisible by the prime.
Here's an example: With 6 characters you can make 266=308915776 unique strings, so a suitable prime number could be 308915753. This function therefore will generate over 300.000.000 unique codes:
function encode($num) {
$scrambled = (240049382*$num + 37043083) % 308915753;
return base_convert($scrambled, 10, 26);
}
Make sure that you run this on 64bit PHP though, otherwise the multiplication will overflow. On 32bit you'll have to use bcmath. The codes generated for the numbers 1 through 9 are:
n89a2d
hdh4jo
biopb9
5o6k2k
3eek5
k8m9aj
ee4424
8jbojf
2ojjb0
All that's left is filling in the initial 0s that are sometimes missing and replacing the letters and numbers so that none of the forbidden characters are produced.
As you can see, there's no obvious pattern, but someone with some time on their hands, enough motivation and with access to a few of this codes will be able to find out what's going on. A safer alternative is using an encryption algorithm with a small block size, such as Skip32.

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.

Categories