• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php中preg正则函数使用_php

php 搞代码 7年前 (2018-06-21) 86次浏览 已收录 0个评论

1.preg_match和preg_match_all的区别
preg_match和 preg_match_all区别是preg_match只匹配一次。而preg_match_all全部匹配,直到字符串结束。例:

 string 'abcde' (length=5)  array (size=1)   0 =>     array (size=3)       0 => string 'abcde' (length=5)       1 => string 'abcde' (length=5)       2 => string 'abcde' (length=5)  */ ?>

2.贪婪模式和非贪婪模式的区别
如:String str=”abcaxc”;
  Patter p=”ab*c”;
贪婪匹配:正则表达式一般趋向于最大长度匹配,也就是所谓的贪婪匹配。如上面使用模式p匹配字符串str,结果就是匹配到:abcaxc(ab*c)。

http://www.gaodaima.com/51387.htmlphp中preg正则函数使用_php

非贪婪匹配:就是匹配到结果就好,较少的匹配字符。如上面使用模式p匹配字符串str,结果就是匹配到:abc(ab*c)。
例:

 string 'http://www.baidu/.com?url=www.sina.com' (length=38)   1 => string '//www.baidu/.com?url=www.sina.' (length=30)  array (size=2)   0 => string 'http://www.baidu/.com' (length=21)   1 => string '//www.baidu/.' (length=13)  */ ?>

3.preg_match_all参数PREG_PATTERN_ORDER(默认)和PREG_SET_ORDER的区别

]+>(.*)]+>U",     "start: this is a testend",     $out1); var_dump($out1);  echo('PREG_SET_ORDER'); preg_match_all("<[^>]+>(.*)]+>U",     "start: this is a testend",     $out2, PREG_SET_ORDER); var_dump($out2);  /* PREG_PATTERN_ORDER array (size=2)   0 =>     array (size=3)       0 => string 'start: ' (length=14)       1 => string 'this is a test' (length=21)       2 => string 'end' (length=10)   1 =>     array (size=3)       0 => string 'start: ' (length=7)       1 => string 'this is a test' (length=14)       2 => string 'end' (length=3)  PREG_SET_ORDER array (size=3)   0 =>     array (size=2)       0 => string 'start: ' (length=14)       1 => string 'start: ' (length=7)   1 =>     array (size=2)       0 => string 'this is a test' (length=21)       1 => string 'this is a test' (length=14)   2 =>     array (size=2)       0 => string 'end' (length=10)       1 => string 'end' (length=3)  */ ?>

延伸阅读:preg_match_all使用实例

欢迎大家阅读《php中preg正则函数使用_php》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php中preg正则函数使用_php
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址