phone的正则表达式在类中有效


regular expression for phone valid at class

我必须使用以下表达式使手机有效

00-972-598-195-871

我把这个功能做成

    private function phoneVald($phone) {
    return ereg("/^[0-9]{2}-[0-9]{3}-[0-9]{3}-[0-9]{3}-[0-9]{3}$/",$phone);
}

这就是表达式的条件

public function setPhone($phone) {
    if ($this-> phoneVald($phone)) {
        $this-> phone = $phone;
    } else {
        echo "<br />Bad Phone Number";
    }
}

当我称之为

$mine = new frindContInfo(00-972-598-195-871);

我的电话号码不正确

我的代码有问题吗!?

您的regex是对的,请尝试通过这种方式从friendContInfo类调用method。您还可以使用__construct()在对象实例化上设置$phone

看这里http://php.net/manual/en/language.oop5.decon.php

    class frindContInfo {
    public function setPhone($phone){
      if ($this-> phoneVald($phone)) {
         $this-> phone = $phone;
         } else {
         echo "<br />Bad Phone Number";
         }
      }
    private function phoneVald($phone) 
     {
      return ereg("/^[0-9]{2}-[0-9]{3}-[0-9]{3}-[0-9]{3}-[0-9] 
      {3}$/",$phone);
     }
 }
$object = new frindContInfo();
$object->setPhone('00-972-598-195-871');