tags:

views:

64

answers:

2

I need to match phone numbers that :

  • start with 010 or 012 or 016 or 019

  • Consist of 10 numbers exactly

can you please help me on how to match numbers using PHP and regex

thanks

+4  A: 
return preg_match('/^01[0269]\\d{7}$/', $theStringToTest);

This will match 0, 1, one of (0, 2, 6, 9), and then any 7 numbers (3+7==10). The ^ means start of string and $ means end of string.

KennyTM
when applying this it gave me 0 no results although the string to test contain numbersexample : $dido = preg_match('/^01[0269]\\d{7}$/',$kokii);echo $dido;
NetCaster
@Net: What is in `$kokii`? Is there some trailing new lines or spaces in it?
KennyTM
thanks problem solved
NetCaster
A: 

I think I'd use ^01[0269][0-9]{7}$

Jerry Coffin