5

my code is:

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

i get false? why?

xdazz
  • 149,740
  • 33
  • 229
  • 258
artwl
  • 3,182
  • 4
  • 32
  • 52

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
  • 149,740
  • 33
  • 229
  • 258
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
  • 161,074
  • 33
  • 239
  • 294