简体   繁体   English

电话的正则表达式在课堂上有效

[英]regular expression for phone valid at class

i have to make valid for mobile phone with this expression 我必须使用这个表达式使手机有效

00-972-598-195-871

i make this function 我做这个功能

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

and this is the condition for the expression 这是表达的条件

public function setPhone($phone) {

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

when i call it 当我打电话给它

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

i got Bad Phone Number 我收到了错误的电话号码

is there any problem with my code !? 我的代码有什么问题!

Your regex is right , try this way to call your method from friendContInfo class. 你的regex是正确的,尝试这种方式从friendContInfo类调用你的method you can use also __construct() to set your $phone on object instantiation. 您还可以使用__construct()在对象实例化上设置$phone

see here http://php.net/manual/en/language.oop5.decon.php 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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM