网站排名快速上升,哪些网站容易做seo优化,怎么做网站框架,石家庄建筑工程造价信息网任务#xff1a;匹配一个函数名或者变量名#xff0c;如果碰到alpha#xff0c;numeric#xff0c;_以外的全部不允许通过。 实验1#xff1a; ?php
//第一个字符不符合就直接退出正则匹配
$str %abcscript%d;
var_dump(preg_match(/^(\w*)$/, $str, $matches));
va…任务匹配一个函数名或者变量名如果碰到alphanumeric_以外的全部不允许通过。 实验1 ?php
//第一个字符不符合就直接退出正则匹配
$str %abcscript%d;
var_dump(preg_match(/^(\w*)$/, $str, $matches));
var_dump($matches);
#########output########
#int(0)
#array(0) {
#}
########################匹配到
$str1 abcscriptd123_;
var_dump(preg_match(/^(\w*?)$/, $str1, $matches));
var_dump($matches);
#########output########
#int(1)
#array(2) {
# [0]
# string(14) abcscriptd123_
# [1]
# string(14) abcscriptd123_
#}
########################中间有不匹配模式的
$str2 acd%acd;
var_dump(preg_match(/^(\w*?)/, $str2, $matches));
var_dump($matches);
#########output########
#int(1)
#array(2) {
# [0]
# string(0)
# [1]
# string(0)
#}
#####################
//检查一个字符串里面仅包含字母数字或者下划线 第一个的结果显而易见preg_match返回0第二个的结果如预期是全串都符合并匹配到第三个的结果有些出人意料那为什么preg_match返回1而$matches未如预期一样包含匹配到的acd呢 再做一个实验实验2 ?php
#中间有不匹配模式的
$str2 acd%acd;
var_dump(preg_match(/^(\w*)/, $str2, $matches));
var_dump($matches);
#########output########
#int(1)
#array(2) {
# [0]
# string(3) acd
# [1]
# string(3) acd
#}
##################### 实验2的结果这次可以匹配到符合条件的部分子串 acd 了。 对比结果表明?这个贪婪匹配符起到了很重要的作用但是对其的工作原理仍然不甚明了。需要继续深入理解。 那么如何完成任务要检查一个字符串是否只包含alpha, numeric, _ 结论是: preg_match(/(\w*)/, $str, $matches); 检查$matches[1] $str如果为true则表示该字符串满足条件为false则表示该字符串不满足条件 ?php
$str acd123_;
var_dump(check_word($str));
$str acd%123_;
var_dump(check_word($str));
$str %acd123_;
var_dump(check_word($str));function check_word($str)
{preg_match(/^(\w*)/, $str, $matches);if($matches[1] $str){return true;} else {return false;}
} 输出 bool(true)
bool(false)
bool(false) 任务把ubb中img标签的内容找出来[img]100.png[/img] 目标熟悉正则表达式中()的用法 代码 ?php$str [img]100[/img]test.png[img]1000[/img];
preg_match_all(/\[img\](.*?)\[\/img\]/, $str, $matches);
var_dump($matches);输出 array(2) {[0]array(2) {[0]string(14) [img]100[/img][1]string(15) [img]1000[/img]}[1]array(2) {[0]string(3) 100[1]string(4) 1000}
}任务把[img]100[/img]提取出来满足两个要求能够提取100并且能够提取出[img]100[/img]这样的模式 目标熟悉正则表达式中()的用法 代码 ?php$str [img]100[/img]test.png[img]1000[/img];
preg_match_all(/(\[img\](.*?)\[\/img\])/, $str, $matches);
var_dump($matches); 输出 array(3) {[0]array(2) {[0]string(14) [img]100[/img][1]string(15) [img]1000[/img]}[1]array(2) {[0]string(14) [img]100[/img][1]string(15) [img]1000[/img]}[2]array(2) {[0]string(3) 100[1]string(4) 1000}
} 理解正则表达式的括号()能提取字符串中的那些匹配的串0号match是整个模式的匹配串1号match是从左往右的第一个()括号中匹配的内容2号match是第二个()括号中匹配的内容以此类推。 关于preg_match_all, 可见另一篇文章http://www.cnblogs.com/helww/p/3248345.html keyword: preg_match preg_match_all转载于:https://www.cnblogs.com/helww/p/3466720.html