I need from regular expression for phone number that can match phones like this:
00354 554-54687 | +354 55445 6584 | +35845344 | +354 458621264
First of number must be '+' or '00' after that can be random count of numbers, spaces and '-'. Thank you
Regular expression for phone number
Page 1 of 19 Replies - 2831 Views - Last Post: 13 January 2011 - 12:55 PM
Replies To: Regular expression for phone number
#2
Re: Regular expression for phone number
Posted 09 January 2011 - 04:34 AM
Cool.
Asking for it here is not going to get you anywhere though.
Show us what you have done yourself - and no I don't mean Google it and copy the first one you find.
And we will probably help you out.
Asking for it here is not going to get you anywhere though.
Show us what you have done yourself - and no I don't mean Google it and copy the first one you find.
And we will probably help you out.
#3
Re: Regular expression for phone number
Posted 09 January 2011 - 06:05 AM

POPULAR
This is not a difficult one as described.
Use ^ to represent the start of your string.
Use [] to encompass the required character sets at the start of your string; OR (|) comes in handy here
Use [] encompass the rest of the permitted character set
Use $ to represent the end of the string
Might I suggest the use of RegExr to assist you?
Use ^ to represent the start of your string.
Use [] to encompass the required character sets at the start of your string; OR (|) comes in handy here
Use [] encompass the rest of the permitted character set
Use $ to represent the end of the string
Might I suggest the use of RegExr to assist you?
#4
Re: Regular expression for phone number
Posted 09 January 2011 - 09:42 AM
Tou could use is_int (or is_numeric).
Or use this:
(copypasta from php website, regex is hard to learn and I do not know how to edit it.)
Or use this:
$number = eregi("^([0-9])+([\.|,]([0-9])*)?$",$val);
(copypasta from php website, regex is hard to learn and I do not know how to edit it.)
#5
Re: Regular expression for phone number
Posted 09 January 2011 - 09:53 AM
creativecoding, on 09 January 2011 - 03:42 PM, said:
Tou could use is_int (or is_numeric).
Or use this:
(copypasta from php website, regex is hard to learn and I do not know how to edit it.)
Or use this:
$number = eregi("^([0-9])+([\.|,]([0-9])*)?$",$val);
(copypasta from php website, regex is hard to learn and I do not know how to edit it.)
eregi is deprecated for a start.
We also shouldn't be encouraging people to just copy and paste code - if he uses that he will have no idea how or why it works.
#6
Re: Regular expression for phone number
Posted 09 January 2011 - 12:07 PM
n_yanev, on 09 January 2011 - 02:37 AM, said:
I need from regular expression for phone number that can match phones like this:
00354 554-54687 | +354 55445 6584 | +35845344 | +354 458621264
First of number must be '+' or '00' after that can be random count of numbers, spaces and '-'. Thank you
00354 554-54687 | +354 55445 6584 | +35845344 | +354 458621264
First of number must be '+' or '00' after that can be random count of numbers, spaces and '-'. Thank you
PHP Code:
preg_match("/[0-9|\+]{0,2}[0-9\- ]*/", $string, $matches);
Regex:
/[0-9|\+]{0,2}[0-9\- ]*/
Explanation:
/ Start delimeter (can be most characters, but the character that is used can't appear in the string)
[0-9|\+] Match any number, or a + sign. 0, 1 or 2 times.
{0,2} Match the previous set between 0 and 2 times, in other words; 0,1 or 2 times.
[0-9\- ] Match any number, dash or space.
* Match the previous set 0 or more times, in other words, as many times as they appear.
/ End delimeter
Tested and working with the example numbers you gave.
EDIT: As moopet pointed out, this code will also match strings such as 9+1, so it's not safe to use in production, but can be used as a starting point
This post has been edited by jordangomm: 09 January 2011 - 02:05 PM
#7
Re: Regular expression for phone number
Posted 09 January 2011 - 01:30 PM
That will also match "9+1" or an empty string or a zillion other combinations. It's a good starting point, though.
#8
Re: Regular expression for phone number
Posted 09 January 2011 - 02:04 PM
#10
Re: Regular expression for phone number
Posted 13 January 2011 - 12:55 PM
Thanks to all 
I try to do it myself and it work.
This is my code
I try to do it myself and it work.
This is my code
/^[\+|\(|\)|\d|\- ]*$/;
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote








|