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

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

WBOY
WBOYOriginal
2016-06-06 19:49:18909browse

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