正则验证类

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版、作者信息和本声明。否则将追究法律责任。http://blog.csdn.net/mayongzhan - 马永占,myz,mayongzhan

<!--下面附带测试-->

<?php
/**
* @name check.php
* @date Sat Nov 24 01:14:28 CST 2007
* @copyright 马永占(MyZ)
* @author 马永占(MyZ)
* @link http://blog.csdn.net/mayongzhan/
*/
/**
* 验证类
* @version 1.0
*/
class Check
{
/**
* 本类调用了我在以前写的计算string长度的函数,如修改或者使用本类请更新此文件
* checkLong()
* checkRight()
*/
/**
* 计算长度,也可以用于顺便验证是否为空验证
*
* @param string $string
* @param int $length
* @param int $length
* @return boolean
*/
public function checkLong($string, $end, $begin = 0)
{
$num = $this->countStr($string, 1);
if ($num <= $end && $num >= $begin) {
return true;
}
else {
return false;
}
}/* end function checkLong */
/**
* 检查输入格式
*
* @param string $value
* @param int $type
* @return boolean
* @author MyZ
*/
public function checkRight($value, $type=7)
{
//$type = 1 是否为 纯 字母数字
//$type = 2 是否为 纯 字母数字._
//$type = 3 是否为 纯 字母
//$type = 4 是否为 纯 小写字母
//$type = 5 是否为 纯 大写字母
//$type = 6 是否为 纯 数字
//$type = 7 是否为 不带 /\'" (这几个为危险字符)
if ($type == 1) {
$regular = '/[^a-z0-9]/i';
}
elseif ($type == 2) {
$regular = '/[^a-z0-9_.]/i';
}
elseif ($type == 3) {
$regular = '/[^a-z]/i';
}
elseif ($type == 4) {
$regular = '/[^a-z]/';
}
elseif ($type == 5) {
$regular = '/[^A-Z]/';
}
elseif ($type == 6) {
$regular = '/[^0-9]/';
}
elseif ($type == 7) {
$regular = '/[\\\\\'\"\/]/'; // \\\\代表\ \/代表/ \'代表' \"代表"
}
else {
$regular = '/[\\\\// \'\"\/]/'; // \\\\代表\ \/代表/ \'代表' \"代表"
}
if (!preg_match($regular, $value)) { //不匹配,证明是
return true;
}
else { //匹配,证明否
return false;
}
}/* end function checkRight */
/**
* 这个是我在以前写的计算string长度的函数,如修改或者使用本类请更新此文件
*
* @param string $str
* @param int $handle
* @param boolean $onlyCh
* @return int
*/
function countStr($str, $handle, $onlyCh = 0)
{
//$handle = 1 按照mb_strlen算 一个中文按照1个字算
//$handle = 2 按照mb_strwidth算 一个中文按照2个字算
//$handle = 3 按照strlen算 一个中文按照3个字算
//$onlyCh = 1 只统计中文
$length = strlen($str);
if ($handle == 3 && $onlyCh == 0) {
return $length;
}
else {
$i = 0;
if ($onlyCh == 1) {
$k = 0;
}
else {
$k = 1;
}
while($i < $length) {
if(preg_match("/^[" . chr(0xa1) . "-" . chr(0xff) . "]+$/", $str[$i])) {
$i += 3;
$n += $handle;
}
else {
$i += 1;
$n += $k;
}
}
return $n;
}
}/* end function countStr */
}/* end class Check */
//测试
$test = new Check();
//测试中英文共6个字的string是否在5到N之间
for ($i=0;$i<20;$i++) {
echo $test->checkLong("马永占MyZ",$i,5)?"yes{$i}<br />":"no{$i}<br />";
}
//测试各种非法字是否正确
$value = array("~`!@#$%^&*()-_=+{}[]:;,.<>?|",
"/",
"\\",
"/\\",
"'",
"\"",
"'\"",
".7",
"_M",
"._7",
"7",
"77",
"777",
"7-7",
"M",
"y",
"MyZ",
"马永占",
"MyZ占777"
);
foreach ($value as $value) {
echo $test->checkRight($value)?"<b>yes{$value}</b><br />":"no{$value}<br />";
}
?>
v
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值