1

i'm new to regex, and i only need one statement.

I want that my statement accepts these numbertyps:

06643823423 could be longer or 0664 3843455 or +43664 4356999

and it's important that these is only one statement.

can anyone help me?

Kara
  • 6,115
  • 16
  • 50
  • 57
mikepenz
  • 12,708
  • 14
  • 77
  • 117
  • See this post, it forms a good introduction to regular expressions - http://stackoverflow.com/questions/4736/learning-regular-expressions – fletcher Aug 23 '10 at 11:32
  • you need to be way more specific, regex is not something that can be made for a phone number, it needs to have matchable points, characteristics as such, please provide info on specific area codes, mobile / landline ? – RobertPitt Aug 23 '10 at 11:33
  • I'd be very wary of using regex with telephone numbers. Most phone numbers have so many different possible working versions it's easy to end up with your validation not accepting real phone numbers. I'd just do a numeric check (with a +()/ check) and max length... – Alex Aug 23 '10 at 11:46

4 Answers4

1

How about:

^\+?[0-9 ]+$

You can use that with preg_match, e.g.

$matches = preg_match("/^\+?[0-9 ]+$/", $telephone_number);
Bobby Jack
  • 15,689
  • 15
  • 65
  • 97
0
$regExp = '/^([+][4][3]|[0]){1}([0-9]{3})([ ]{0,1})([0-9]{7})$/';
$number = "06643823423";

if(!preg_match($regExp,trim($number)))
{
        echo FALSE;
}
else
{                
        echo TRUE;
}
Maulik Vora
  • 2,544
  • 5
  • 28
  • 48
  • 4
    This (`([+][4][3]|[0]){1}`) hurts my eyes. `(\+43|0)` does the exact same ... and is less confusing, IMO. – jensgram Aug 23 '10 at 11:39
  • Thanks :) I had used this regEx before 1 month , at that time It is not worked as expected , so I am using like this. now I will use as you suggested – Maulik Vora Aug 23 '10 at 11:43
  • also, you cant echo false or true haha, there booleans. – RobertPitt Aug 23 '10 at 13:41
  • 1
    @RobertPitt, you can echo booleans, but it's not pretty. True prints as '1', and False prints as ''. – gen_Eric Aug 23 '10 at 14:27
  • 1
    yea you cant echo false is what i meant, why would you want to echo a boolean anyway ? – RobertPitt Aug 23 '10 at 14:37
  • @RobertPitt Its just an example , you can do whatever you want to do there. you can return true or false or can do any further process. It was jus texample, haha. – Maulik Vora Aug 25 '10 at 05:21
0

out ofwhat you have there, the only regex I could come up with is:

$phone_number = '+449287645367';
var_dump(preg_match("/^[\+|0][0-9]{10,12}$/",str_replace(' ','',$phone_number)));
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • jensgram, that was not an error mate, it tests for only the + because it can be +[0-9]{12} as well as 0[0-9]{10} the 12 length is to accommodate the +*44* – RobertPitt Aug 23 '10 at 11:53
  • So you mean `(\+|0)`, I guess. `[+|0]` means `+` or `|` or `0` AFAIK (I may be wrong, though). – jensgram Aug 23 '10 at 12:02
  • No your right, for some reason he online PHP tester I used to to test this strips backslashes so after I copied the working regex it had removed the slash :/ – RobertPitt Aug 23 '10 at 13:38
0

Try \+?\d+\s?\d+

To explain:
\+? - a plus sign (escaped with \, since '+' is a special character in regex). The '?' means '0 or 1 of the preceding characters' making it optional
\d+ - \d means a digit; the plus sign means 1 or more
\s? - \s means a white space character; the ? makes it optional
\d+ - \d means a digit; the plus sign means 1 or more

So this should match 2 or more digits, with an optional + sign at the beginning, and an optional space somewhere in the middle.

Mike C
  • 3,077
  • 22
  • 29