开发者

regex to check for a valid regex pattern in php

I am attempting to write a validation class in php and have hit a snag. I am using the following code to check a value against a defined regex pattern but when I try to run the script I receive an error message saying:

"Warning: preg_match() [function.preg-match]: Empty regular expression in C:\wamp\www\valid\Class.Validate.php on line 109"

Can anyone spot my mistake ?

   public function is_regex($var, $expression, $msg)
   {
      if (trim($var) <> "") {
          $Valid = (bool)preg_match($expression, $var);
          if ($Valid == false) {
              return $msg;
          }
      }开发者_JAVA百科     
   }

and I am using this to call the function

array('Type'=>'Regex','Var'=>$_POST['test'], 'Expression'=>'[A-Za-z]{1,20}$','Msg'=>'Expression does not match')    


As far as I see, you need to enclose regexp into enclosure characters, for example | so your checking array will be like

array('Type'=>'Regex','Var'=>$_POST['test'], 'Expression'=>'|[A-Za-z]{1,20}$|','Msg'=>'Expression does not match')  

You can also define character in function, but in this case if you'll have this character in regexp, you'll need to escape it

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜