php - preg_match
任务:匹配一个函数名或者变量名,如果碰到alpha,numeric,_以外的全部不允许通过。
实验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
php - preg_match的更多相关文章
- preg_match()漏洞
今天大哥丢了一道题过来. <?php $str = intval($_GET['id']); $reg = preg_match('/\d/is', $_GET['id']); //有0-9的数 ...
- php preg_match 过滤字符
$f = preg_match("/g3watches/",$date[0]['desc']); if ($f='1') { $this->error(L('不好意思,输入有 ...
- php preg_match($p, $str, $match)方法简介
方法作用:匹配指定的正则表达式并将结果放在$match数组中 代码示例: $p = '/name:([\\ws]+)/'; $str = "name:steven jobs"; p ...
- php 函数preg_match、preg_match_all ,以及正则表达式规则
<?php $str = 'php is the best language phhhhp is'; $part = '/ph{1,}p/'; echo preg_match($part, $s ...
- ***PHP preg_match正则表达式的使用
第一,让我们看看两个特别的字符:‘^’和‘$’他们是分别用来匹配字符串的开始和结束,以下分别举例说明 : "^The": 匹配以 "The"开头的字符串; &q ...
- php中preg_match用户名正则实例
例子,字母.数字和汉字 代码如下 复制代码 if(preg_match("/[ '.,:;*?~`!@#$%^&+=)(<>{}]|]|[|/|\|"||/& ...
- preg_match_all, preg_match
int preg_match(string $pattern, string $subject[, $arr][, int $flags]);$pattern 正则表达式$subject: 要搜索的字 ...
- PHP函数补完:preg_match()
preg_match — 进行正则表达式匹配. 语法:int preg_match ( string $pattern , string $subject [, array $matches [, i ...
- PHP 正则表达式匹配 preg_match 与 preg_match_all 函数
--http://www.5idev.com/p-php_preg_match.shtml 正则表达式在 PHP 中的应用 在 PHP 应用中,正则表达式主要用于: 正则匹配:根据正则表达式匹配相应的 ...
- PHP preg_match正则表达
在php中preg_match()函数是用来执行正则表达式的一个常用的函数,下面我来给大家详细介绍preg_match使用方法. 函数用法 int preg_match_all ( string pa ...
随机推荐
- mis 系统的开发具备的条件
MIS的开发方式有自行开发.委托开发.联合开发.购买现成软件包进行二次开发几种形式.一般来说根据企业的技术力量.资源及外部环境而定. 补充: 管理信息系统的开发策略不可行的开发方法:组织结构法,机械的 ...
- 对dpkg: error processing package xxx (--configure) 的处理
这个问题是在在安装Deepdive时候碰到的,具体来说在执行 sudo apt-get update碰到的sudo mv /var/lib/dpkg/info/ /var/lib/dpkg/info_ ...
- Gulp构建
整理自 https://markpop.github.io/2014/09/17/Gulp%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B/ 已经有package.json 第 ...
- C# TextBox中的Validating与Validated事件[转]
validating事件发生的时候,值还没有被真正存入,所以是空的,而validated事件发生的时候,值已经存入,所以能取出:一个相当于值的前因,一个相当于值的后果.也就是说,可以用validati ...
- Ubuntu安装Mysql过程及远程问题解决
ubuntu下执行 sudo apt-get instlll mysql-server sudo apt-get install mysql-client 安装过程中会有文字界面设置密码 牢记密码 M ...
- 关于cocos2dx的C++调用创建项目
我使用的是cocos2dx-2.1.4版本+cygwin,其实主要是为了配合公司项目,所以用了低版本的cocos2dx 假设已经配置环境成功: 按照对应的要求输入包名,项目名,以及TargetId,就 ...
- php基础(五)日期
PHP Date() 函数 date(format,timestamp) 参数 描述 format 必需.规定时间戳的格式. timestamp 可选.规定时间戳.默认是当前时间和日期. 注释:时间戳 ...
- Hoffmann树
数据压缩编码 先把两棵二叉树简化成叶子结点带权的二叉树,图的每个结点之间带有权值 结点的路径长度: 从根结点到该结点的路径上的连接数. 树的路径长度: 树中每个叶子结点的路径长度之和. 结点带权路径长 ...
- LeetCode OJ 41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- php 积少成多!