Page 1 of 1

Why does this regex not work in PHP?

Posted: Wed Jul 19, 2006 11:58 am
by KindredHyperion
Can someone tell me why the following regular expression doesn't work with
PHP, although it does work with almost every single other regex validator
I've found on the internet, including
http://www.sweeting.org/mark/html/revalid.php and
http://www.koralsoft.com/regextester/ ...??

(\+\d{1,3} ?)?(\(\d{1,5}\)|\d{1,5}) ?\d{3} ?\d{0,7}

The code I'm using is:

Code: Select all

<?php
if (ereg('(\+\d{1,3} ?)?(\(\d{1,5}\)|\d{1,5}) ?\d{3} ?\d{0,7}','020 1234
5678'))
    echo "match";
else
    echo "no match";
?>
Which, as far as I can see, should work on the supplied dummy phone number.
For some reason it doesn't though, which may just be a problem with my
coding - bear in mind I haven't written anything in PHP for several years so
I'm a bit rusty.

Thanks!

Adam

Posted: Wed Jul 19, 2006 12:05 pm
by sweatje

Code: Select all

php -r "var_dump(preg_match('~(\+\d{1,3} ?)?(\(\d{1,5}\)|\d{1,5}) ?\d{3} ?\d{0,7}~','020 1234 5678'));"

int(1)

Posted: Wed Jul 19, 2006 12:11 pm
by KindredHyperion
Aha! I forgot the tildes. There we go!

Thanks for your help!