您好,欢迎来到思海网络,我们将竭诚为您提供优质的服务! 诚征网络推广 | 网站备案 | 帮助中心 | 软件下载 | 购买流程 | 付款方式 | 联系我们 [ 会员登录/注册 ]
促销推广
客服中心
业务咨询
有事点击这里…  531199185
有事点击这里…  61352289
点击这里给我发消息  81721488
有事点击这里…  376585780
有事点击这里…  872642803
有事点击这里…  459248018
有事点击这里…  61352288
有事点击这里…  380791050
技术支持
有事点击这里…  714236853
有事点击这里…  719304487
有事点击这里…  1208894568
有事点击这里…  61352289
在线客服
有事点击这里…  531199185
有事点击这里…  61352288
有事点击这里…  983054746
有事点击这里…  893984210
当前位置:首页 >> 技术文章 >> 文章浏览
技术文章

php中preg正则函数使用

添加时间:2014-11-30 21:27:05  添加: 思海网络 
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)。
非贪婪匹配:就是匹配到结果就好,较少的匹配字符。如上面使用模式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、正则函数

分享到:

顶部 】 【 关闭
版权所有:佛山思海电脑网络有限公司 ©1998-2024 All Rights Reserved.
联系电话:(0757)22630313、22633833
中华人民共和国增值电信业务经营许可证: 粤B1.B2-20030321 备案号:粤B2-20030321-1
网站公安备案编号:44060602000007 交互式栏目专项备案编号:200303DD003  
察察 工商 网安 举报有奖  警警  手机打开网站