几种常见的正则表达式验证(From ThinkPHP/Lib/Think/Core/Model.class.php)

/**
+----------------------------------------------------------
* 使用正则验证数据
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $value 要验证的数据
* @param string $rule 验证规则
+----------------------------------------------------------
* @return boolean
+----------------------------------------------------------
*/
public function regex($value,$rule) {
$validate = array(
'require'=> '/.+/',
'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
'url' => '/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/',
'currency' => '/^\d+(\.\d+)?$/',
'number' => '/^\d+$/',
'zip' => '/^[1-9]\d{5}$/',
'integer' => '/^[-\+]?\d+$/',
'double' => '/^[-\+]?\d+(\.\d+)?$/',
'english' => '/^[A-Za-z]+$/',
);
// 检查是否有内置的正则表达式
if(isset($validate[strtolower($rule)]))
$rule = $validate[strtolower($rule)];
return (bool)preg_match($rule,$value);
}

你可能感兴趣的:(thinkphp)