您当前的位置: 首页 > 网站编程 > PHP教程 > php 惯用验证类及正则

php 惯用验证类及正则

作者:不详 来源:网络 发布时间: 2014-07-31 18:19 点击:
php 常用验证类及正则 正则表达式在遇到新的时候将会不断更新 include ValidateParameterConfig.php; class Validation { private static function getRexp($rexp) { $_rexp = array ( 'letter_number'='/^[0-9A-Za-z]+$/',//只有字母数字包括大小写 'account'='/^[0-9

php 惯用验证类及正则

  php 常用验证类及正则

  正则表达式在遇到新的时候将会不断更新

  include "ValidateParameterConfig.php";

  class Validation

  {

  private static function getRexp($rexp)

  {

  $_rexp = array (

  'letter_number'=>'/^[0-9A-Za-z]+$/',//只有字母数字包括大小写

  'account'=>'/^[0-9A-Za-z_]+$/',//只有字母数字下划线包括大小写

  'ids'=>'/^[0-9]+(,[0-9]+)*$/',//验证多个id以','分割的类型 例如'1,2,3,4,5'

  'number'=>'/^[0-9]+$/',//只可以使数字

  'personal_card'=>'/^[0-9A-Za-z]+$/',//身份证

  'email'=>'/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/',//邮箱

  'date'=>'/^((((19|20)d{2})-(0?(1|[3-9])|1[012])-(0?[1-9]|[12]d|30))|(((19|20)d{2})-(0?[13578]|1[02])-31)|(((19|20)d{2})-0?2-(0?[1-9]|1d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$/'//日期,包含了闰年

  );

  if (isset($_rexp[$rexp])) {

  return $_rexp[$rexp];

  } else {

  return $rexp_not_defind;

  }

  }

  public static function validate($data, $config)

  {

  $_config = ValidateParameterConfig::getConfig($config);

  $k = self::allowExist($data, $_config);

  if ($k !== null) {

  return $k;

  }

  foreach($_config as $k=>$c) {

  if (isset($data[$k]))

  {

  if(isset($c['rexp']))

  {

  if (self::vRexp( $data[$k], $c['rexp']) === false) {

  return $k;

  }

  }

  if (isset($c['length']))

  {

  if (self::vLength($data[$k], $c['length']))

  {

  return $k;

  }

  }

  if (isset($c['min_length']))

  {

  if (!self::vMinLength($data[$k], $c['min_length']))

  {

  return $k;

  }

  }

  if (isset($c['max_length']))

  {

  if (!self::vMaxLength($data[$k], $c['max_length']))

  {

  return $k;

  }

  }

  }

  }

  return null;

  }

  private static function allowExist($data, $config)

  {

  foreach ($config as $k=>$v)

  {

  if (!isset($v['allow_exist']) || $v['allow_exist'] == true) {

  if (!isset($data[$k]))

  {

  return $k;

  }

  }

  }

  return null;

  }

  public static function vRexp($data, $rexp) {

  $_rexp = self::getRexp($rexp);

  if (preg_match($_rexp, $data) == false) {

  return false;

  }

  return true;

  }

  public static function vLength($data, $l) {

  if (strlen(trim($data)) == $l) {

  return false;

  }

  return true;

  }

  public static function vMinLength($data, $l) {

  if (strlen(trim($data)) < $l) {

  return false;

  }

  return true;

  }

  public static function vMaxLength($data, $l) {

  if (strlen(trim($data)) > $l) {

  return false;

  }

  return true;

  }

  public static function vLetterNumber($data) {

  if (preg_match(self::getRexp('letter_number'), $data) == false) {

  return false;

  }

  return true;

  }

  public static function vLetterNumber_($data) {

  if (preg_match(self::getRexp('letter_number_'), $data) == false) {

  return false;

  }

  return true;

  }

  public static function vNumber($data) {

  if (preg_match(self::getRexp('number'), $data) == false) {

  return false;

  }

  return true;

  }

  public static function vEmail($data) {

  if (preg_match(self::getRexp('email'), $data) == false) {

  return false;

  }

  return true;

  }

  class ValidateParameterConfig

  {

  public static function getConfig ($key)

  {

  //'allow_exist'=>true或allow_exist不存在表示该参数必传

  $_config = array(

  'test'=>array(

  'letter_number'=>array('rexp'=>'letter_number', 'allow_exist'=>true),

  'number'=>array('rexp'=>'number', 'min_length'=>1'allow_exist'=>false),

  'account'=>array('rexp'=>'account', 'max_length'=>20),

  )

  );

  if (isset($_config[$key])) {

  return $_config[$key];

  } else {

  return $config_not_defind;

  }

  }

  }

  使用例子:

  从应用端发过来的参数为

  $arr = array('letter_number'=>'abc123', 'account'=>'acb1234_');

  //参数验证

  $_msg = Validation::validate($arr, 'test');

  if ($_msg !== null) {

  return $_msg . ' is invalid parameter';

  }
分享到:
本文"php 惯用验证类及正则"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4