php正则表达式等编程知识总结

上传人:飞*** 文档编号:47490682 上传时间:2018-07-02 格式:PDF 页数:38 大小:694.19KB
返回 下载 相关 举报
php正则表达式等编程知识总结_第1页
第1页 / 共38页
php正则表达式等编程知识总结_第2页
第2页 / 共38页
php正则表达式等编程知识总结_第3页
第3页 / 共38页
php正则表达式等编程知识总结_第4页
第4页 / 共38页
php正则表达式等编程知识总结_第5页
第5页 / 共38页
点击查看更多>>
资源描述

《php正则表达式等编程知识总结》由会员分享,可在线阅读,更多相关《php正则表达式等编程知识总结(38页珍藏版)》请在金锄头文库上搜索。

1、Php进阶定界符 ; 字符域 ; 修饰符 ; 限定符 ; 脱字符 ; 通配符 (正向预查, 反向预查 ); 反向引用 ; 惰性匹配 ;注释 ; 零字符宽定位我们什么时候使用正则表达式呢?不是所有的字符操作都用正则就好了,php 在某些方面用正则反而影响效率。当我们遇到复杂文本数据的解析时候,用正则是比较好的选择。优点正则表达式在处理复杂字符操作的时候,可以提高工作效率,也在一定程度节省你的代码量。缺点我们在使用正则表达式的时候,复杂的正则表达式会加大代码的复杂度,让人很难理解。所以我们有的时候需要在正则表达式内部添加注释。通用模式定界符,通常使用 “/“做为定界符开始和结束, 也可以使用 “#

2、“ 。什么时候使用“#“ 呢?一般是在你的字符串中有很多“/“ 字符的时候,因为正则的时候这种字符需要转义,比如uri 。使用 “/“ 定界符的代码如下. $regex = /http:/(w.+)/(w+)/(w+).html$/i;$str = http:/ = array();if(preg_match($regex, $str, $matches)var_dump($matches);echo “n“;preg_match 中的 $matches0 将包含与整个模式匹配的字符串。使用 “#“ 定界符的代码如下. 这个时候对 “/“ 就不转义 ! $regex = #http:/(w.+

3、)/(w+)/(w+).html$#i;$str = http:/ = array();if(preg_match($regex, $str, $matches)var_dump($matches);echo “n“;修饰符 : 用于改变正则表达式的行为。我们看到的 (/http:/(w.+)/(w+)/(w+).html/i)中的最后一个 “i“就是修饰符 , 表示忽略大小写,还有一个我们经常用到的是“x“ 表示忽略空格。贡献代码 : $regex = /HELLO/;$str = hello word;$matches = array();if(preg_match($regex, $st

4、r, $matches)echo No i:Valid Successful!,“n“;if(preg_match($regex.i, $str, $matches)echo YES i:Valid Successful!,“n“;字符域 :w用方括号扩起来的部分就是字符域。限定符 : 如w3,5或者 w*或者 w+ 这些 w 后面的符号都表示限定符。现介绍具体意义。3,5 表示 3 到 5 个字符。 3, 超过 3 个字符, ,5 最多 5 个, 3 三个字符。 * 表示 0到多个, + 表示 1 到多个。脱字符号放在字符域 ( 如:w)中表示否定 ( 不包括的意思) “反向选择”放在表达式

5、之前,表示以当前这个字符开始。(/n/i,表示以 n 开头 ) 。注意,我们经常管“ 叫“跳脱字符 “。用于转义一些特殊符号,如“.“,“/“ 通配符 (lookarounds):断言某些字符串中某些字符的存在与否!lookarounds分两种 :lookaheads(正向预查 ?=) 和 lookbehinds(反向预查 ?) 调用方式 (?P= 组名 ) $regex = /(?Pchuanshanjia)sIss(?P=author)/i;$str = author:chuanshanjia Is chuanshanjia;$matches = array();if(preg_match

6、($regex, $str, $matches)var_dump($matches);echo “n“;运行结果格式 :限定符 ? 原理 :“?“ :如果前面有限定符,会使用最小的数据。如“* ”会取 0 个,而“ +”会取 1个,如过是 3,5会取 3 个。先看下面的两个代码: 代码 1. $regex = /heL*/i;$str = heLLLLLLLLLLLLLLLL;if(preg_match($regex, $str, $matches)var_dump($matches);echo “n“;结果 1. 代码 2 $regex = /heL*?/i;$str = heLLLLLLL

7、LLLLLLLLL;if(preg_match($regex, $str, $matches)var_dump($matches);echo “n“;结果 2 代码 3, 使用“ +”$regex = /heL+?/i;$str = heLLLLLLLLLLLLLLLL;if(preg_match($regex, $str, $matches)var_dump($matches);echo “n“;结果 3 代码 4, 使用 3,5 $regex = /heL3,10?/i;$str = heLLLLLLLLLLLLLLLL;if(preg_match($regex, $str, $match

8、es)var_dump($matches);echo “n“;结果 4 正则表达式的注释格式 :(?# 注释内容 ) 用途 :主要用于复杂的注释贡献代码 : 是一个用于连接MYSQL 数据库的正则表达式$regex = /host=(?.*|/ 匹配首尾空格的正则表达式:(s*)|(s*$) String.prototype.trim = function() return this.replace(/(s*)|(s*$)/g, “); 利用正则表达式分解和转换IP 地址:下面是利用正则表达式匹配IP 地址,并将IP 地址转换成对应数值的Javascript程序:function IP2V(i

9、p) re=/(d+).(d+).(d+).(d+)/g /匹配 IP 地址的正则表达式if(re.test(ip) return RegExp.$1*Math.pow(255,3)+RegExp.$2*Math.pow(255,2)+RegExp.$3*255+RegExp.$4*1 else throw new Error(“Not a valid IP address!“) 不过上面的程序如果不用正则表达式,而直接用split函数来分解可能更简单,程序如下:var ip=“10.100.20.168“ ip=ip.split(“.“) alert(“IP值是: “+(ip0*255*25

10、5*255+ip1*255*255+ip2*255+ip3*1) 匹配 Email 地址的正则表达式:w+(-+.w+)*w+(-.w+)*.w+(-.w+)* 匹配网址URL的正则表达式:- ./?% $x prop+) is 3 times slower than a local variable. 递增一个对象属性( 如: $this-prop+)要比递增一个局部变量慢3 倍。23. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one. 递增一个未预定义的局部变量

11、要比递增一个预定义的局部变量慢9至 10 倍。24. Just declaring a global variable without using it in a function also slows things down (by about the same amount as incrementing a local var)。 PHP probably does a check to see if the global exists. 仅定义一个局部变量而没在函数中调用它,同样会减慢速度( 其程度相当于递增一个局部变量) 。PHP大概会检查看是否存在全局变量。25. Method i

12、nvocation appears to be independent of the number of methods defined in the class because I added 10 more methods to the test class (before and after the test method) with no change in performance. 方法调用看来与类中定义的方法的数量无关,因为我( 在测试方法之前和之后都) 添加了 10 个方法,但性能上没有变化。26. Methods in derived classes run faster th

13、an ones defined in the base class. 派生类中的方法运行起来要快于在基类中定义的同样的方法。27. A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar+ operations. A similar method call is of course about 15 $localvar+ operations. 调用带有一个参数的空函数,其花费的时间相当于执行 7 至 8 次的局部变量递增操作。类

14、似的方法调用所花费的时间接近于15 次的局部变量递增操作。28. Surrounding your string by instead of “ will make things interpret a little faster since php looks for variables inside “ ,“ but not inside , . Of course you can only do this when you dont need to have variables in the string. 用单引号代替双引号来包含字符串,这样做会更快一些。 因为 PHP会在双引号包围的

15、字符串中搜寻变量,单引号则不会。当然,只有当你不需要在字符串中包含变量时才可以这么做。29. When echoing strings its faster to separate them by comma instead of dot. Note: This only works with echo, which is a function that can take several strings as arguments. 输出多个字符串时,用逗号代替句点来分隔字符串,速度更快。注意:只有echo 能这么做,它是一种可以把多个字符串当作参数的“函数 “( 译注: PHP手册中说 echo是语言结构,不是真正的函数,故把函数加上了双引号) 。30. A PHP script will be served at least 2-10 times slower than a static HTML page by Apache. Try to use more static HTML pages and

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号