php - Regular Expression to determine if phone number is 'local', 'national', 'mobile', 'premium', etc -


i'm knocking simple php form show telephone call usage on our asterisk system. call information being recorded in database.

i'm familiar regular expressions , php (to degree), have never attempted combine 2 before.

parsing data, want identify outgoing call type, can bill our customer @ correct rate. i'm splitting outgoing calls number of classes, example, 'local', 'national', 'mobile', 'premium', 'international' , 'internal', attempts match first class (i.e. 'local') have failed.

local calls have 9 prefix, optionally followed local area code (01234 in example). followed local number (567890 in example). note local number can't start zero.

so numbers i'm trying match above examples 901234567890 or 9567890.

i use regex such ^9(01234)?[1-9][0-9]+, , testing bash shell, expected results

$ echo 091234567890 | egrep -e '^9(01234)?[1-9][0-9]+' 901234567890 $ echo 9567890 | egrep -e '^9(01234)?[1-9][0-9]+' 9567890 $ echo 908001234567 | egrep -e '^9(01234)?[1-9][0-9]+' $ echo 901987654321 | egrep -e '^9(01234)?[1-9][0-9]+' $ echo 907890123456 | egrep -e '^9(01234)?[1-9][0-9]+' 

so attempting translate work in php, i've come following:

$class='unknown'; if (preg_match('/^9(01234)?[1-9][0-9]+/', $dest))   $class='local call'; // more regex matches follow other classes 

now, when throw following list of numbers @ above

901234567890 9567890 908001234567 901987654321 907890123456 

every 1 of numbers appears match regex. doing wrong?

i ran test on numbers , worked:

$nums = array('901234567890', '9567890'    , '908001234567', '901987654321', '907890123456'); foreach ($nums $num) {    if (preg_match('/^9(01234)?[1-9][0-9]+/', $num)) {       echo "$num passes\n";    }    else {       echo "$num fails\n";    } } 

901234567890 passes 9567890 passes 908001234567 fails 901987654321 fails 907890123456 fails 

in code, passing set variable, maybe need check it's being set..

in addition that, checking phone numbers against regex doomed endeavor. that's going bit far, difficult. phone number parsing class @ job on 1400 lines long , still doesn't work in cases.


Comments