一.email
1
<?php
$preg_email='/^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@([a-zA-Z0-9]+[-.])+([a-z]{2,5})$/ims';
# /^串:表示以串为开头 +:表示至少有1个 串$:表示以串为结尾
// /ims: i不区分大小写 m多行模式 s点号包括换行符内的所有字符
$email='123456@qq.com';
echo "[0] ",preg_match($preg_email,$email),"<br>";
$e1="123.456.abc@123.com";
echo "[1] ",preg_match($preg_email,$e1),"<br>";
$e1="456.abc@123.com.cn";
echo "[2] ",preg_match($preg_email,$e1),"<br>";
$e1="123 456.abc@123.com";
echo "[a] ",preg_match($preg_email,$e1),"<br>";
$e1="123 456.abc@123.";
echo "[b] ",preg_match($preg_email,$e1),"<br>";
?>
二.phone
1
<?php
$preg_phone='/^1[34578]\d{9}$/ims';
//对输入字符串的校验 不是长字符串发现子串的匹配
$phone='13651121610';
echo "[1]",preg_match($preg_phone,$phone)."<br>";
$p1='15912345678';
echo "[2]",preg_match($preg_phone,$p1)."<br>";
$p1='23651121610';
echo "[a]",preg_match($preg_phone,$p1)."<br>";
?>
2
<?php
$call='02312345678';
$preg_call='/^(0\d{2,3})?(\d{7,8})$/ims';
echo "[1]",preg_match($preg_call,$call),"<br>";
echo "[2]",preg_match($preg_call,'12345678')."<br>";
$pre='/^0((10|2[0-9])-?\d{8}|[3-9]\d{2}-?\d{7,8})$/ims';
echo "[3]",preg_match($pre,"02012345678"),"<br>";
echo "[3]",preg_match($pre,"020-12345678"),"<br>";
echo "[4]",preg_match($pre,"075512345678"),"<br>";
echo "[4]",preg_match($pre,"0755-12345678"),"<br>";
echo "[a]",preg_match($pre,"0201234567"),"<br>";
echo "[a]",preg_match($pre,"020-1234567"),"<br>";
echo "[a]",preg_match($pre,"020123456789"),"<br>";
echo "[a]",preg_match($pre,"020-123456789"),"<br>";
echo "[b]",preg_match($pre,"0755123456789"),"<br>";
echo "[b]",preg_match($pre,"0755-123456789"),"<br>";
?>
三.id
1
<?php
$IDCard='';
$preg_card='/(^\d{15})$|(^\d{17}([0-9]|X))$/isu';
# /u 表示按unicode(utf-8)匹配(主要针对多字节比如汉字)
if(preg_match($preg_card,$IDCard)){echo 1;}else{echo 0;};echo "<br>";
echo preg_match($preg_card,"12345620101231123x")."<br>"
?>
四.html
1.
<?php
$html = '<p>This is a paragraph.</p>';
$html.='<a href="https://www.baidu.com">Click here</a><br>';
echo "[0]",$html,"<br>";
echo "[1]",htmlspecialchars_decode($html)."<br>";
echo "[2]\n",htmlspecialchars($html)."<br>","<br>";
preg_match('/<p>(.*?)<\/p>/ms', $html, $matches);
echo "[3]<br>";
var_dump($matches); echo "<br>";
echo "<br>[4]";
echo $matches[0],"\t",$matches[1],"<br>";
echo "<br>[5]";
foreach ($matches as $match) {
echo $match;
}
preg_match('/<a .*?<\/a>/ms', $html, $matches);
echo "<br><br>[a]",$matches[0];
echo "<br>",var_dump($matches),"<br>";
foreach ($matches as $match) {
echo htmlspecialchars($match),$match,trim($match);
}
preg_match_all('/<.*?>.*?<\/\w+>/ms', $html, $matches);
echo "<br><br>[a1]";
echo "<br>",var_dump($matches),"结束 ";
foreach ($matches[0] as $match) {
echo htmlspecialchars($match);
}
preg_match('/<.*?>.*?<\/\w+>/ms', $html, $matches);
echo "<br><br>[a2]";
echo var_dump($matches);
?>