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 ...
随机推荐
- oracle中110个常用函数介绍
1. ASCII 返回与指定的字符对应的十进制数; SQL> select ascii(A) A,ascii(a) a,ascii(0) zero,ascii( ) space from dua ...
- Adding DOM elements to document
1.JavaScript 添加DOM Element 执行效率比较: 抄自:http://wildbit.com/blog/2006/11/21/javascript-optimization-add ...
- angular指令
转自:http://www.cnblogs.com/rohelm/p/4051437.html 对于指令,可以把它简单的理解成在特定DOM元素上运行的函数,指令可以扩展这个元素的功能. 首先来看个完整 ...
- MYSQL导入中文数据乱码的四种解决办法
方法一:通过增加参数 --default-character-set = utf8 解决乱码问题 方法一:通过增加参数 --default-character-set = utf8 解决乱码问题 my ...
- 【1】ubuntu 安装docker
官方支持安装docker的Ubuntu版本: ubuntu trusty 14.04(LTS) (64位) ubuntu precise 12.04(LTS) (64位) ubuntu raring ...
- Java反序列化漏洞分析
相关学习资料 http://www.freebuf.com/vuls/90840.html https://security.tencent.com/index.php/blog/msg/97 htt ...
- iOS开发传感器相关
手机里面内置了很多的传感器,例如:光传感器,湿度传感器,温度传感器,距离传感器等等 //开发传感器相关的东西必须使用真机 //在螺旋仪和加速计所有两种方式push和pull的方式,push的方式是时时 ...
- PBO
#include <GL/glew.h> #include <GL/freeglut.h> #include <iostream> GLuint pboID[]; ...
- 驱动力—— 通信引擎(上)—— ESFramework 4.0 进阶(03)
在ESFramework 4.0 进阶(02)-- 核心:消息处理的骨架流程一文中我们详细介绍了ESFramework中消息处理的骨架流程,并且我们已经知道,ESFramework中的所有通信引擎使用 ...
- MVC中发生System.Data.Entity.Validation.DbEntityValidationException验证异常的解决方法
发生System.Data.Entity.Validation.DbEntityValidationException这个异常的时候,如果没有用特定的异常类去捕捉,是看不到具体信息的. 通常都是用Sy ...