php正则表达式—工具类的开发
2015-06-20 11:23
721 查看
<?php
class regexTool
{
private $validate = array(
'require' => '/.+/',
'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
'url' => '/^((http|https|ftp):\/\/)?(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/',
'currency' => '/^\d+(\.\d+)?$/',
'number' => '/^\d+$/',
'zip' => '/^\d{6}$/',
'integer' => '/^[-\+]?\d+$/',
'double' => '/^[-\+]?\d+(\.\d+)?$/',
'english' => '/^[A-Za-z]+$/',
'qq' => '/^\d{5,11}$/',
'mobile' => '/^1(3|4|5|7|8)\d{9}$/',
);
private $returnMatchResult = false; // 是否返回匹配结果
private $fixMode = null; // 修正模式
private $matches = array(); // 匹配结果
private $isMatch = false; // 是否匹配到结果
function __construct($returnMatchResult = false,$fixMode = null)
{
$this->returnMatchResult = $returnMatchResult;
$this->fixMode = $fixMode;
}
/**
* 匹配目标字符串并返回结果
* @param string or array $pattern 匹配规则
* @param string or array $subject 目标字符串
*/
private function regex($pattern,$subject)
{
if(array_key_exists(strtolower($pattern),$this->validate))
{
$pattern = $this->validate[$pattern].$this->fixMode;
}
$this->returnMatchResult ?
preg_match_all($pattern,$subject,$this->matches) :
$this->isMatch = preg_match($pattern, $subject) === 1;
return $this->getRegexResult();
}
/**
* 根据returnMatchResult成员判断返回何种结果类型并返回结果
*/
private function getRegexResult()
{
if($this->returnMatchResult)
{
return $this->matches;
}
else
{
return $this->isMatch;
}
}
/**
* 切换返回结果
* @param bool $bool 布尔值
*/
public function toggleReturnType($bool = null)
{
if(empty($bool))
{
$this->returnMatchResult = !$this->returnMatchResult;
}
else
{
$this->returnMatchResult = is_bool($bool) ? $bool : (bool)$bool;
}
}
/**
* 设置修正模式
@ param string $fixMode 修正模式
*/
public function setFixMode($fixMode)
{
$this->fixMode = $fixMode;
}
/**
* 检测是否为空
* @param string $str 目标字符串
*/
public function noEmpty($str)
{
return $this->regex('require',$str);
}
/**
* 检测是否为有效的email
* @param string $email 需要检测的email
*/
public function isEmail($email)
{
return $this->regex('email',$email);
}
/**
* 检测是否为手机号
* @param string $mobile 需要检测的手机号码
*/
public function isMobile($mobile)
{
$this->regex('mobile',$mobile);
}
/**
* 匹配目标字符串并返回结果
* @param string or array $pattern 匹配规则
* @param string or array $subject 目标字符串
*/
public function check($pattern,$subject)
{
return $this->regex($pattern,$subject);
}
}
?>
class regexTool
{
private $validate = array(
'require' => '/.+/',
'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
'url' => '/^((http|https|ftp):\/\/)?(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/',
'currency' => '/^\d+(\.\d+)?$/',
'number' => '/^\d+$/',
'zip' => '/^\d{6}$/',
'integer' => '/^[-\+]?\d+$/',
'double' => '/^[-\+]?\d+(\.\d+)?$/',
'english' => '/^[A-Za-z]+$/',
'qq' => '/^\d{5,11}$/',
'mobile' => '/^1(3|4|5|7|8)\d{9}$/',
);
private $returnMatchResult = false; // 是否返回匹配结果
private $fixMode = null; // 修正模式
private $matches = array(); // 匹配结果
private $isMatch = false; // 是否匹配到结果
function __construct($returnMatchResult = false,$fixMode = null)
{
$this->returnMatchResult = $returnMatchResult;
$this->fixMode = $fixMode;
}
/**
* 匹配目标字符串并返回结果
* @param string or array $pattern 匹配规则
* @param string or array $subject 目标字符串
*/
private function regex($pattern,$subject)
{
if(array_key_exists(strtolower($pattern),$this->validate))
{
$pattern = $this->validate[$pattern].$this->fixMode;
}
$this->returnMatchResult ?
preg_match_all($pattern,$subject,$this->matches) :
$this->isMatch = preg_match($pattern, $subject) === 1;
return $this->getRegexResult();
}
/**
* 根据returnMatchResult成员判断返回何种结果类型并返回结果
*/
private function getRegexResult()
{
if($this->returnMatchResult)
{
return $this->matches;
}
else
{
return $this->isMatch;
}
}
/**
* 切换返回结果
* @param bool $bool 布尔值
*/
public function toggleReturnType($bool = null)
{
if(empty($bool))
{
$this->returnMatchResult = !$this->returnMatchResult;
}
else
{
$this->returnMatchResult = is_bool($bool) ? $bool : (bool)$bool;
}
}
/**
* 设置修正模式
@ param string $fixMode 修正模式
*/
public function setFixMode($fixMode)
{
$this->fixMode = $fixMode;
}
/**
* 检测是否为空
* @param string $str 目标字符串
*/
public function noEmpty($str)
{
return $this->regex('require',$str);
}
/**
* 检测是否为有效的email
* @param string $email 需要检测的email
*/
public function isEmail($email)
{
return $this->regex('email',$email);
}
/**
* 检测是否为手机号
* @param string $mobile 需要检测的手机号码
*/
public function isMobile($mobile)
{
$this->regex('mobile',$mobile);
}
/**
* 匹配目标字符串并返回结果
* @param string or array $pattern 匹配规则
* @param string or array $subject 目标字符串
*/
public function check($pattern,$subject)
{
return $this->regex($pattern,$subject);
}
}
?>
相关文章推荐
- php单一接口的实现方法
- php文件读取方法实例分析
- php写入、删除与复制文件的方法
- php文件操作之小型留言本实例
- php post大量数据时发现数据丢失问题解决方法
- PHP文件生成的图片无法使用CDN缓存的解决方法
- 手机访问本地php项目遇到的问题及解决
- 手机访问本地php项目遇到的问题及解决
- TI C66x DSP 系统events及其应用 - 5.8(ISTP)
- USB UMS MTP设置过程 (一)
- Dreamweaver CS6 PHP代码提示
- ThinkPHP3.2 加载过程(三)
- ThinkPHP3.2 加载过程(二)
- ThinkPHP3.2 加载过程(一)
- thinkphp3.2入口文件
- ThinkPHP3.2 常量参考
- ThinkPHP 3.2 开发过程
- php编码
- php运行环境最小化安装
- thinkphp对数据库操作有哪些内置函数