I need a reg exp to check for telephone number, the telephone number goes like this:
400-1889-8989
So the telephone number should include 0-9 and dash(-) only, and it has to start and end with number. Don't know how to write a reg exp... any help would be appreciated! Thanks.
This is a strict interpretation of your requirements (include 0-9 and dash (-) only, and it has to start and end with number):
/^[0-9][0-9-]*[0-9]$/
Do realize that this leaves the door open to a variety of inputs that will not even remotely resemble a phone number, e.g. 00, 0-----9, 666666666666666666666666 and so on.
This regex will only return true if the number is like "3-4-4", I mean "932-3434-4232" or "534-3342-4233"
^[0-9]{3}-[0-9]{4}-[0-9]{4}$
Going by the thought that all telephone numbers would be of the form 000-0000-0000, that is 3 digits followed by -, followed by 4 digits, then a - again and another 4 digits.
The regex will be like this
/[0-9]{3}-[0-9]{4}-[0-9]{4}/
Related
:)
We would like to set a special condition (based on PHP Preg_match regular expression) to validates a number on our form.
That “number field” need, at first, only contain a max of 13 numbers (and only numbers. No letters or anything else).
The very first number need to be (only) “1” or “2” (not anything else)
The 4rd and 5rd number represent (the 2 numbers combinated) the “Month of birth” of someone, so the 4rd number need to be "0" or "1", and the 5rd need to be between "1" and "9".
Really appreciates if you can help us for that, to have the good “syntax” for the regular expression in PHP Preg_match to validates that field on our form! :)
Thanks to the community for your support and help!
Regards
Here is the literal regex pattern you have described to us:
^[12]\d{2}(?:0[1-9]|1[0-2])\d{8}$
Sample script:
$input = "1231212345678";
if (preg_match("/^[12]\d{2}(?:0[1-9]|1[0-2])\d{8}$/", $input)) {
echo "MATCH";
}
This regex pattern says to:
^ from the start of the string
[12] match 1 or 2 as the first digit
\d{2} then match any digits in the 2nd and 3rd position
(?:0[1-9]|1[0-2]) match 01, 02, ..., 12 as the two digit month
\d{8} then match any other 8 digits
$ end of string
I'm trying to "validate" some input from a form on the backend but I kinda got stuck with the required regex. Basically, I want to get a number, no leading zeroes, with an optional decimal and an optional case insensitive thousands notation (1,000 = 1k, 1,000,000 = 1m, 1 billion = 1b ...)
These should match/validate:
12.39
12.389k
99.1003b
40.1m
These should fail.
0.0
12.00b
1e3
2^5
0xFF
12.6z
asdf
So far I've tried this:
^[1-9]\d*(?:\.\d+)?(?:[kmbt])?$
It works but I'm thinking maybe someone could solve this a bit more elegantly than I did.
[Edit] without thousand separator:
/^(?:[1-9][0-9]*(?:\.[0-9]*[1-9])?|0\.[0-9]*[1-9])[kbmt]?$/i
demo
[Old answer] with thousand separator:
You can do it with:
/^(?:[1-9][0-9]{0,2}(?:(,?)[0-9]{3})?(?:\1[0-9]{3})*(?:\.[0-9]*[1-9])?|0\.[0-9]*[1-9])[kbmt]?$/i
demo
details:
^
(?: # numbers >= 1
[1-9][0-9]{0,2}
(?:(,?)[0-9]{3})? # capture an eventual thousand separator
(?:\1[0-9]{3})*
# decimal
(?:\.[0-9]*[1-9])?
| # numbers < 1
0\.[0-9]*[1-9]
)[kbmt]?$
Update in REGEX.
Below regex will not leading zero allow special characters and characters without specified for notation.
Below regex will allow decimal and integer with thousand and other notation.
^[1-9]\d*(\.[0-9]+)?(?:[kmbtKMBT])?$
Please check working demo : http://www.regexpal.com/?fam=95995
The below should be what you're after:
/(\b[1-9][\d\,\.]+([kmbt]?))(\s)/g
It matches 12.00b however... Here is an example of it's use.
Group 1 will contain your exact match.
I'm trying to remove / detect phone numbers from messages between users of my marketplace website (think eBay does something similar)
this is the code I'm using:
$string = preg_replace('/([0-9]+[\- ]?[0-9]+)/', '', $string);
BUT... it's too aggressive and it does strip away any number with 2 or more numerals... how can set a limit of say 7 numbers instead?
to be more precise the phone numbers can be any format like
3747657654
374-7657654
374-765-7654
(374)765-7654
etc...(i cannot predict what the users will write depending of their habits)
Try this regular expression :
/([0-9]+[\- ]?[0-9]{6,})/
changed to match your samples:
Regex101
That would depend on the exact requirements as now you have 1 or more numbers followed by an optional - or space followed by 1 or more numbers again.
If you wanted for example at least 2 numbers before the space or - followed by at least 5 numbers, you could use something like:
$string = preg_replace('/([0-9]{2,}[\- ]?[0-9]{5,})/', '', $string);
^^^^ Here you can specify mininimum / maximum
^^^^ Here you can specify mininimum / maximum
You can try something like this:
$string = preg_replace('/(?<![0-9]|[0-9]-)[0-9](?:[- ]?[0-9]){6}(?!-?[0-9])/', '', $string);
The lookarounds are here to avoid numbers with more than 7 digits, but if you want something more specific, you should provide an example string.
It is impossible to determine whether a number of X digits (where X is a valid phone number length) is a phone number or something else without some sort of context intelligence happening. A simple regex can't determine the difference between "call me at 3453456" and "call me when you've flown 3453456 miles".
Therefore trying to catch phone numbers without any formatting (just straight digits) with a regex is hopeless, pure and simple. Attempting to do so is only holding you back from finding a regex that can find formatted/semi-formatted numbers. What you should be going for here is "get the obvious and as many others as possible with minimal false positives...but recognize I can't get them all."
For that I'd recommend this:
/1?[ \-]?\(?([0-9]{3})?\)?[ \-]?([0-9]{3})[ \-]([0-9]{4})/g
It should not get the first three, but get all the rest in this list:
no-match: 3747657654
no-match: 444444444444444
no-match: 7657654
match: 374-765-7654
match: 1-374-765-7654
match: (374)765-7654
match: (374) 765 7654
match: 765-7654
match: 1 (374) 765 7654
match: 1(374)765 7654
Is there a function or a easy way to strip down phone numbers to a specific format?
Input can be a number (mobile, different country codes)
maybe
+4917112345678
+49171/12345678
0049171 12345678
or maybe from another country
004312345678
+44...
Im doing a
$mobile_new = preg_replace("/[^0-9]/","",$mobile);
to kill everything else than a number, because i need it in the format 49171 (without + or 00 at the beginning), but i need to handle if a 00 is inserted first or maybe someone uses +49(0)171 or or inputs a 0171 (needs to be 49171.
so the first numbers ALWAYS need to be countryside without +/00 and without any (0) between.
can someone give me an advice on how to solve this?
You can use
(?:^(?:00|\+|\+\d{2}))|\/|\s|\(\d\)
to match most of your cases and simply replace them with nothing. For example:
$mobile = "+4917112345678";
$mobile_new = preg_replace("/(?:^(?:00|\+|\+\d{2}))|\/|\s|\(\d\)/","",$mobile);
echo $mobile_new;
//output: 4917112345678
regex101 Demo
Explanation:
I'm making use of OR here, matching each of your cases one by one:
(?:^(?:00|\+|\+\d{2})) matches 00, + or + followed by two numbers at the beginning of your string
\/ matches a / anywhere in the string
\s matches a whitspace anywhere in the string (it matches the newline in the regex101 demo, but I suppose you match each number on its own)
\(\d\) matches a number enclosed in brackets anywhere in the string
The only case not covered by this regex is the input format 01712345678, as you can only take a guess what the country specific prefix can be. If you want it to be 49 by default, then simply replace each input starting with a single 0 with the 49:
$mobile = "01712345678";
$mobile_new = preg_replace("/^0/","49",$mobile);
echo $mobile_new;
//output: 491712345678
This pattern (49)\(?([0-9]{3})[\)\s\/]?([0-9]{8}) will split number in three groups:
49 - country code
3 digits - area code
8 digits - number
After match you can construct clean number just concatnating them by \1\2\3.
Demo: https://regex101.com/r/tE5iY3/1
If this not suits you then please explain more precisely what you want with test input and expected output.
I recommend taking a look at LibPhoneNumber by Google and its port for PHP.
It has support for many formats and countries and is well-maintained. Better not to figure this out yourself.
https://github.com/giggsey/libphonenumber-for-php
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$usNumberProto = $phoneUtil->parse("+1 650 253 0000", "US");
working on a project right now where we have large amount of text strings that we must localize phone numbers and make them clickable for android phones.
The phone numbers can be in different formats, and have different text before and after them. Is there any easy way of detecting every kind of phone number format? Some library that can be used?
Phone numbers can show like, has to work with all these combinations. So that the outcome is like
number
+61 8 9000 7911
+2783 207 5008
+82-2-806-0001
+56 (2) 509 69 00
+44 (0)1625 500125
+1 (305)409 0703
031-704 98 00
+46 31 708 50 60
Perhaps something like this:
/(\+\d+)?\s*(\(\d+\))?([\s-]?\d+)+/
(\+\d+)? = A "+" followed by one or more digits (optional)
\s* = Any number of space characters (optional)
(\(\d+\))? = A "(" followed by one or more digits followed by ")" (optional)
([\s-]?\d+)+ = One or more set of digits, optionally preceded by a space or dash
To be honest, though, I doubt that you'll find a one-expression-to-rule-them-all. Telephone numbers can be in so many different formats that it's probably impractical to match any possible format with no false positives or negatives.
Not sure that there is a library for that. Hmmmm. Like any international amenities, telephone numbers are standardised and there should be a format defining telephone numbers as well. E.164 suggests recommended telephone numbers: http://en.wikipedia.org/wiki/E.164 . All open-source decoding libraries are built from reading these standard formats, so it should be of some help if you really cant find any existing libs
I guess this might do it for these cases?
preg_replace("/(\+?[\d-\(\)\s]{7,}?\d)/", 'number', $str);
Basicly I check if it may start on +. It doesn't have to. Then I check if it got numbers, -, (, ) and spaces with at least 8 cases so it doesn't pick low non-phone numbers.
Try the following:
preg_match_all('/\+?[0-9][\d-\()-\s+]{5,12}[1-9]/', $text, $matches);
or:
preg_match_all('/(\+?[\d-\(\)\s]{8,20}[0-9]?\d)/', $text, $matches);