转义字符2017-01-23 本站 guaitu
d 匹配任何数字字符,同[0-9]<?php
header("content-type: text/html;charset=utf-8");
$mode="/d/";
$string="Beijing 2017"; //匹配成功
//下面的字符串,不包含数字,匹配失败
//$string="www.bianceng.cn";
if(preg_match($mode,$string)){
echo "匹配成功!";
}else{
echo "不匹配!";
}
?>
D 匹配包含任何非数字字符的字符串,同[^0-9]<?php
header("content-type: text/html;charset=utf-8");
$mode="/D/";
$string="Beijing 2017"; //匹配成功
//下面的字符串,不包含数字以外的字符,匹配失败
//$string="2020";
if(preg_match($mode,$string)){
echo "匹配成功!";
}else{
echo "不匹配!";
}
?>
w
匹配任何包含小写字母、大写字母、数字或者下划线的字符串,等价于
[a-zA-Z0-9_]<?php
header("content-type: text/html;charset=utf-8");
$mode="/w/";
$string="www.bianceng.cn"; //匹配成功
//下面的字符串,不包含小写字母、大写字母、数字或者下划线,匹配失败
//$string="%#";
if(preg_match($mode,$string)){
echo "匹配成功!";
}else{
echo "不匹配!";
}
?>
W 匹配任何包含下划线、字母、数字及下划线以外的字符的字符串<?php
header("content-type: text/html;charset=utf-8");
$mode="/W/";
$string="%#"; //匹配成功
//下面的字符串,不包含小写字母、大写字母、数字或者下划线以外的字符,匹配失败
//$string="beijingHELLO2008";
if(preg_match($mode,$string)){
echo "匹配成功!";
}else{
echo "不匹配!";
}
?>
s: 匹配任何空白字符<?php
header("content-type: text/html;charset=utf-8");
$mode="/s/";
$string="Beijing 2017"; //匹配成功
//下面的字符串,不包含空白字符,匹配失败
//$string="www.bianceng.cn";
if(preg_match($mode,$string)){
echo "匹配成功!";
}else{
echo "不匹配!";
}
?>
S: 匹配任何非空白字符:表示到达了单词的边界,如果没有到达,就不匹配<?php
header("content-type: text/html;charset=utf-8");
$mode="/php/";
$string="php is good";//到了空格,认为是到达边界,匹配成功
//$string="phpp"; // 不匹配
if(preg_match($mode,$string)){
echo "匹配成功!";
}else{
echo "不匹配!";
}
?>
B: 表示没有到达边界<?php
header("content-type: text/html;charset=utf-8");
$mode="/phpB/";
$string="phpp";//没有到达边界,匹配成功
//$string="php"; // 不匹配
if(preg_match($mode,$string)){
echo "匹配成功!";
}else{
echo "不匹配!";
}
?>
:匹配正则中的特殊字符<?php
header("content-type: text/html;charset=utf-8");
$mode="/ph+p/";
$string="ph+p";//匹配成功
if(preg_match($mode,$string)){
echo "匹配成功!";
}else{
echo "不匹配!";
}
?>
URL: http://www.bianceng.cn/webkf/PHP/201701/50534.htm