search
Homephp教程php手册PHP数据验证常用正则表达收集

PHP数据验证常用正则表达收集

Jun 06, 2016 pm 07:49 PM
phpsizeCommonly usedcollectdataregularruleverify

1.用户名(规则大小写字母下划线和数字,缺中文) $string = userNaME4234432_; if (preg_match('/^[a-z/d_]{4,28}$/i', $string)) { echo example 1 successful.; } 2.固定电话验证(缺3~4位区号,7~8位电话,分机) $string = (032)555-5555; if (preg_m

1.用户名(规则大小写字母下划线和数字,缺中文)

$string = "userNaME4234432_";
if (preg_match('/^[a-z/d_]{4,28}$/i', $string)) {
echo "example 1 successful.";
}

2.固定电话验证(缺3~4位区号,7~8位电话,分机)

$string = "(032)555-5555";
if (preg_match('/^(/(?[2-9]{1}[0-9]{2}/)?|[0-9]{3,3}[-. ]?)[ ][0-9]{3,3}[-. ]?[0-9]{4,4}$/', $string)) {
echo "example 2 successful.";
}

3.邮件验证

$string = "first.last@domain.co.uk";
if (preg_match(
'/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',
$string)) {
echo "example 3 successful.";
}

4.邮政编码(?什么规则的邮编难道国家区号+地区邮编)

$string = "55324-4324";
if (preg_match('/^[0-9]{5,5}([- ]?[0-9]{4,4})?$/', $string)) {
echo "example 4 successful.";
}

5.ip地址验证(基本没用)

$string = "255.255.255.0";
if (preg_match(
'^(?:25[0-5]|2[0-4]/d|1/d/d|[1-9]/d|/d)(?:[.](?:25[0-5]|2[0-4]/d|1/d/d|[1-9]/d|/d)){3}$',
$string)) {
echo "example 5 successful.";
}

6.颜色输入验证

$string = "#666666";
if (preg_match('/^#(?:(?:[a-f/d]{3}){1,2})$/i', $string)) {
echo "example 6 successful.";
}

7.C风格注释匹配

$string = "/* commmmment */";
if (preg_match('/^[(/*)+.+(*/)]$/', $string)) {
echo "example 7 successful.";
}

8.一种日期格式验证

$string = "10/15/2007";
if (preg_match('/^/d{1,2}///d{1,2}///d{4}$/', $string)) {
echo "example 8 successful.";
}

Statement
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool