Home Backend Development PHP Tutorial PHP implements the method of regular regular verification helper public class

PHP implements the method of regular regular verification helper public class

May 23, 2018 pm 02:33 PM
helper php verify

这篇文章主要介绍了PHP实现的常规正则验证helper公共类,结合完整实例形式分析了php针对常规的电话、手机、邮箱、账号等进行正则验证的操作技巧,需要的朋友可以参考下

主要代码功能: 弥补平时项目对于验证功能这块的不严谨。具体细分的常规验证, 手机号/电话/小灵通验证, 字符串长度区间合法验证, 邮箱验证, 使用正则验证数据.

/**
 *
 *
 * 常规验证helper公共类
 *
 *
 */
class CheckForm
{
  //手机号/电话/小灵通 验证
  public function Mobile_check($mobile,$type = array())
  {
    /**
    * 手机号码
    * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
    * 联通:130,131,132,152,155,156,185,186
    * 电信:133,1349,153,180,189
    */
    $res[1]= preg_match('/^1(3[0-9]|5[0-35-9]|8[0-9])\\d{8}$/', $mobile);
    /**
    * 中国移动:China Mobile
    11   * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
    */
    $res[2]= preg_match('/^1(34[0-8]|(3[5-9]|5[017-9]|8[0-9])\\d)\\d{7}$/', $mobile);
    /**
    * 中国联通:China Unicom
    * 130,131,132,152,155,156,185,186
    */
    $res[3]= preg_match('/^1(3[0-2]|5[256]|8[56])\\d{8}$/', $mobile);
    /**
    * 中国电信:China Telecom
    * 133,1349,153,180,189
    */
    $res[4]= preg_match('/^1((33|53|8[09])[0-9]|349)\\d{7}$/', $mobile);
    /**
    * 大陆地区固话及小灵通
    * 区号:010,020,021,022,023,024,025,027,028,029
    * 号码:七位或八位
    */
    $res[5]= preg_match('/^0(10|2[0-5789]|\\d{3})-\\d{7,8}$/', $mobile);
    $type = empty($type) ? array(1,2,3,4,5) : $type;
    $ok = false;
    foreach ($type as $key=>$val)
    {
      if ($res[$val])
      {
        $ok = true;
      }
      continue;
    }
    if ( $mobile && $ok )
    {
      return true;
    } else{
      return false;
    }
  }
  //字符串长度区间合法验证
  public function Strlength_check($str, $min=NULL, $max=NULL)
  {
    preg_match_all("/./u", $str, $matches);
    $len = count($matches[0]);
    if(is_null($min) && !empty($max) && $len < $max){
      return false;
    }
    if(is_null($max) && !empty($min) && $len > $min){
      return false;
    }
    if ($len < $min || $len > $max) {
      return false;
    }
    return true;
  }
  //邮箱验证
  public static function isEmail($str)
  {
    if (!$str) {
      return false;
    }
    return preg_match(&#39;#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is&#39;, $str) ? true : false;
  }
  /**
  * 使用正则验证数据
  * @access public
  * @param string $rule 验证规则
  * @param string $value 要验证的数据
  * @return boolean
  */
  public function regex($rule,$value) {
    $validate = array(
    //字段必须,不能为空
    &#39;require&#39; => &#39;/\S+/&#39;,
    //邮箱验证
    &#39;email&#39;  => &#39;/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/&#39;,
    //url验证
    &#39;url&#39;  => &#39;/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(?:[\/\?#][\/=\?%\-&~`@[\]\&#39;:+!\.#\w]*)?$/&#39;,
    //货币验证
    &#39;currency&#39; => &#39;/^\d+(\.\d{0,2})?$/&#39;,
    //数字验证
    &#39;number&#39; => &#39;/^[-\+]?\d+(\.\d+)?$/&#39;,
    //zip验证
    &#39;zip&#39;  => &#39;/^\d{6}$/&#39;,
    //整数验证
    &#39;integer&#39; => &#39;/^[-\+]?\d+$/&#39;,
    //浮点数验证
    &#39;double&#39; => &#39;/^[-\+]?\d+(\.\d+)?$/&#39;,
    //英文验证
    &#39;english&#39; => &#39;/^[A-Za-z]+$/&#39;,
    &#39;gt0&#39; => &#39;/^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/&#39;,
    //合法帐号
    &#39;account&#39; => &#39;/^[a-zA-Z][a-zA-Z0-9_]{1,19}$/&#39;
    );
    // 检查是否有内置的正则表达式
    if(isset($validate[strtolower($rule)]))
    $rule = $validate[strtolower($rule)];
    return preg_match($rule,$value)===1;
  }
  function CheckPwd($pwd,$min=NULL, $max=NULL)
  {
  if (strlen($pwd)>$max || strlen($pwd)<$min || preg_match("/^\d*$/",$pwd) || preg_match("/^[a-z]*$/i",$pwd))
  {
    return false;
  }
  return true;
  }
}
Copy after login

is_null() 检测变量是否为 NULL。

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

php遍历解析xml字符串的方法_php技巧

PHP递归遍历多维数组实现无限分类的方法_php技巧

php简单复制文件的方法_php技巧

The above is the detailed content of PHP implements the method of regular regular verification helper public class. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles