5

my code is:

<?php
    $phone = 18311111111;
    if(ereg("^\d{11}$",$phone)){
        echo "true";
    } else {
        echo "false";
    }
?>

i get false? why?

xdazz
  • 158,678
  • 38
  • 247
  • 274
artwl
  • 3,502
  • 6
  • 38
  • 53

2 Answers2

4

Because ereg does not support \d, you need use [0-9] instead.

And ereg is deprecated, use preg_match instead, then you could use \d.

if(preg_match("/^\d{11}$/",$phone)){
    echo "true";
} else {
    echo "false";
}
xdazz
  • 158,678
  • 38
  • 247
  • 274
0

For what it's worth, you shouldn't use ereg (deprecated) nor preg_match for such a simple test; you can use ctype_digit():

if (ctype_digit($phone)) {
    // $phone consists of only digits
} else {
    // non-digit characters were found in $phone
}
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309