9 Replies - 4335 Views - Last Post: 13 January 2011 - 12:55 PM Rate Topic: -----

#1 n_yanev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 24-October 10

Regular expression for phone number

Posted 09 January 2011 - 03:37 AM

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
Is This A Good Question/Topic? 0
  • +

Replies To: Regular expression for phone number

#2 KingCuddles  Icon User is offline

  • D.I.C Regular

Reputation: 176
  • View blog
  • Posts: 496
  • Joined: 20-December 08

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.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

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?
Was This Post Helpful? 5
  • +
  • -

#4 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: Regular expression for phone number

Posted 09 January 2011 - 09:42 AM

Tou could use is_int (or is_numeric).


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.)
Was This Post Helpful? 0
  • +
  • -

#5 KingCuddles  Icon User is offline

  • D.I.C Regular

Reputation: 176
  • View blog
  • Posts: 496
  • Joined: 20-December 08

Re: Regular expression for phone number

Posted 09 January 2011 - 09:53 AM

View Postcreativecoding, on 09 January 2011 - 03:42 PM, said:

Tou could use is_int (or is_numeric).


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.
Was This Post Helpful? 3
  • +
  • -

#6 jordangomm  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 24
  • Joined: 12-January 10

Re: Regular expression for phone number

Posted 09 January 2011 - 12:07 PM

View Postn_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


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

Was This Post Helpful? 3
  • +
  • -

#7 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

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.
Was This Post Helpful? 1
  • +
  • -

#8 jordangomm  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 24
  • Joined: 12-January 10

Re: Regular expression for phone number

Posted 09 January 2011 - 02:04 PM

View Postmoopet, on 09 January 2011 - 12:30 PM, said:

That will also match "9+1" or an empty string or a zillion other combinations. It's a good starting point, though.


Oh crap, didn't think of checking things that it shouldn't match. Whoops, I'll see if I can improve on that one.
Was This Post Helpful? 0
  • +
  • -

#9 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Regular expression for phone number

Posted 10 January 2011 - 02:01 PM

View PostJackOfAllTrades, on 09 January 2011 - 07:05 AM, said:

Might I suggest the use of RegExr to assist you?


That's really cool. I've been meaning to learn RegEx anyway, and that will come in handy. :^:
Was This Post Helpful? 0
  • +
  • -

#10 n_yanev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 24-October 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

/^[\+|\(|\)|\d|\- ]*$/;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1