<?php
PHP正则表达式
\d匹配0-9
$zz='/\d/';
$string='我爱喝9,你来不来';
if(preg_match($zz, $string,$matches)){
echo '匹配到了,结果为:';
var_dump($matches);
}else{
echo '匹配不到';
}
echo "<br>";
\D匹配一个非0-9的值
$zz='/\D/';
$string='4546546种33413543';
if(preg_match($zz,$string,$matches)){
echo "匹配成功,结果为:";
var_dump($matches);
}else{
echo "匹配失败";
}
echo "<br>";
\w匹配到一个a-zA-Z0-9_
$zz='/\w/';
$string='新中_国万岁';
if(preg_match($zz,$string,$matches)){
echo "匹配成功,结果为:";
var_dump($matches);
}else{
echo "匹配失败";
}
echo "<br>";
\W匹配一个非0-9a-zA-Z_
$zz='/\W/';
$string='haksdfhkasdha 35656ashfkashhdQJKkja';
if(preg_match($zz,$string,$matches)){
echo "匹配成功,结果为:";
var_dump($matches);
}else{
echo "匹配失败";
}
echo "<br>";
\s匹配所有空白字符\n \t \r
$zz='/\s/';
$string='新中_国
万岁';
if(preg_match($zz,$string,$matches)){
echo "匹配成功,结果为:";
var_dump($matches);
}else{
echo "匹配失败";
}
echo "<br>";
\S匹配所有非空白字符\n \t \r
$zz='/\S/';
$string='新中_国
万岁';
if(preg_match($zz,$string,$matches)){
echo "匹配成功,结果为:";
var_dump($matches);
}else{
echo "匹配失败";
}
echo "<br>";
[]指定范围的原子
$zz='/[0-5]\w+/';
$string='6a';
$string1='1C';
if(preg_match($zz,$string,$matches)){
echo '匹配成功,结果为:';
var_dump($matches);
}else{
echo '匹配失败';
}
echo "<br>";
if(preg_match($zz,$string1,$matches)){
echo '匹配成功,结果为:';
var_dump($matches);
}else{
echo '匹配失败';
}
echo "<br>";
$zz='/[a-zA-Z0-9_]\w/';
$string='ac';
$string1='9A';
if(preg_match($zz,$string,$matches)){
echo "匹配成功,结果为:";
var_dump($matches);
}else{
echo "匹配失败";
}
echo "<br>";
if(preg_match($zz,$string1,$matches)){
echo "匹配成功,结果为:";
var_dump($matches);
}else{
echo "匹配失败";
}
echo "<br>";
$zz='/[abc]\d+/';
$string='a9';
$string1='b1';
$string2='c5';
$string3='d4';
if(preg_match($zz,$string,$matches)){
echo "匹配成功,结果为:";
var_dump($matches);
}else{
echo "匹配失败";
}
echo "<br>";
if(preg_match($zz,$string1,$matches)){
echo "匹配成功,结果为:";
var_dump($matches);
}else{
echo "匹配失败";
}
echo "<br>";
if(preg_match($zz,$string2,$matches)){
echo "匹配成功,结果为:";
var_dump($matches);
}else{
echo "匹配失败";
}
echo "<br>";
if(preg_match($zz,$string3,$matches)){
echo "匹配成功,结果为:";
var_dump($matches);
}else{
echo "匹配失败";
}
echo "<br>";
[^字符]:不匹配指定区间的字符
$zz='/[^\w]/';
$string='aaaaaab311dd';
$string1='!$*$!@#&$@#($$)()%_#%(@';
echo "<br>";
if(preg_match($zz,$string,$matches)){
echo '匹配成功,结果为:';
var_dump($matches);
}else{
echo "匹配失败";
}
echo "<br>";
if(preg_match($zz,$string1,$matches)){
echo '匹配成功,结果为:';
var_dump($matches);
}else{
echo "匹配失败";
}