<?php
$p = "/chinese/m";
$str = "i am a chinese people,\n you alose is a chinese people";
$math = "";
if (preg_match_all($p, $str,$math)) {
echo '该字符串符合这个规则:/chinese/m,匹配结果为:';
print_r($math);
}else{
echo '该字符串不符合这个规则:/chinese/m';
}
?>
运行结果为:
该字符串符合这个规则:/chinese/m,匹配结果为:Array ( [0] => Array ( [0] => chinese [1] => chinese ) )
这里要注意的是一定要用preg_match_all否则用preg_match它只会匹配一行
接下来我们运行下效果
<?php
$p = "/chinese/m";
$str = "i am a chinese people,\n you alose is a chinese people";
$math = "";
if (preg_match($p, $str,$math)) {
echo '该字符串符合这个规则:/chinese/m,匹配结果为:';
print_r($math);
}else{
echo '该字符串不符合这个规则:/chinese/m';
}
?>
运行结果为:
该字符串符合这个规则:/chinese/m,匹配结果为:Array ( [0] => chinese )
其实/m在此也算多此一举,因为preg_match_all就是表示多行匹配了
<?php
$p = "/chinese/";
$str = "i am a chinese people,\n you alose is a chinese people";
$math = "";
if (preg_match_all($p, $str,$math)) {
echo '该字符串符合这个规则,匹配结果为:';
print_r($math);
}else{
echo '该字符串不符合这个规则';
}
?>
运行结果其实是一样的,结果为:
该字符串符合这个规则,匹配结果为:Array ( [0] => Array ( [0] => chinese [1] => chinese ) )
<?php
$p = "/chinese ./s";
$str = "i am a chinese \n people, you alose is a chinese good people";
$math = "";
if (preg_match_all($p, $str,$math)) {
echo '该字符串符合这个规则,匹配结果为:';
print_r($math);
}else{
echo '该字符串不符合这个规则';
}
?>
运行结果如下:
该字符串符合这个规则,匹配结果为:Array ( [0] => Array ( [0] => chinese [1] => chinese g ) )
说明第一个chinese 后面的字符是换行也匹配到了,这说明了s的意思就是.要包含换行符,接下来
我们去掉s,看下最终的结果
<?php
$p = "/chinese ./";
$str = "i am a chinese \n people, you alose is a chinese good people";
$math = "";
if (preg_match_all($p, $str,$math)) {
echo '该字符串符合这个规则,匹配结果为:';
print_r($math);
}else{
echo '该字符串不符合这个规则';
}
?>
运行结果如下:
该字符串符合这个规则,匹配结果为:Array ( [0] => Array ( [0] => chinese g ) )
<?php
$p = "/chinese/m";
$str = "i am a chinese people,\n you alose is a chinese people";
$math = "";
if (preg_match_all($p, $str,$math)) {
echo '该字符串符合这个规则:/chinese/m,匹配结果为:';
print_r($math);
}else{
echo '该字符串不符合这个规则:/chinese/m';
}
?>
运行结果为:
该字符串符合这个规则:/chinese/m,匹配结果为:Array ( [0] => Array ( [0] => chinese [1] => chinese ) )
这里要注意的是一定要用preg_match_all否则用preg_match它只会匹配一行
接下来我们运行下效果
<?php
$p = "/chinese/m";
$str = "i am a chinese people,\n you alose is a chinese people";
$math = "";
if (preg_match($p, $str,$math)) {
echo '该字符串符合这个规则:/chinese/m,匹配结果为:';
print_r($math);
}else{
echo '该字符串不符合这个规则:/chinese/m';
}
?>
运行结果为:
该字符串符合这个规则:/chinese/m,匹配结果为:Array ( [0] => chinese )
其实/m在此也算多此一举,因为preg_match_all就是表示多行匹配了
<?php
$p = "/chinese/";
$str = "i am a chinese people,\n you alose is a chinese people";
$math = "";
if (preg_match_all($p, $str,$math)) {
echo '该字符串符合这个规则,匹配结果为:';
print_r($math);
}else{
echo '该字符串不符合这个规则';
}
?>
运行结果其实是一样的,结果为:
该字符串符合这个规则,匹配结果为:Array ( [0] => Array ( [0] => chinese [1] => chinese ) )
<?php
$p = "/chinese ./s";
$str = "i am a chinese \n people, you alose is a chinese good people";
$math = "";
if (preg_match_all($p, $str,$math)) {
echo '该字符串符合这个规则,匹配结果为:';
print_r($math);
}else{
echo '该字符串不符合这个规则';
}
?>
运行结果如下:
该字符串符合这个规则,匹配结果为:Array ( [0] => Array ( [0] => chinese [1] => chinese g ) )
说明第一个chinese 后面的字符是换行也匹配到了,这说明了s的意思就是.要包含换行符,接下来
我们去掉s,看下最终的结果
<?php
$p = "/chinese ./";
$str = "i am a chinese \n people, you alose is a chinese good people";
$math = "";
if (preg_match_all($p, $str,$math)) {
echo '该字符串符合这个规则,匹配结果为:';
print_r($math);
}else{
echo '该字符串不符合这个规则';
}
?>
运行结果如下:
该字符串符合这个规则,匹配结果为:Array ( [0] => Array ( [0] => chinese g ) )