php中preg正則函數使用_PHP教程

來源:互聯網
上載者:User

php中preg正則函數使用


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";
貪婪匹配:Regex一般趨向於最大長度匹配,也就是所謂的貪婪匹配。如上面使用模式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_ORDERarray (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_ORDERarray (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使用執行個體

http://www.bkjia.com/PHPjc/894184.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/894184.htmlTechArticlephp中preg正則函數使用 1.preg_match和preg_match_all的區別 preg_match和 preg_match_all區別是preg_match只匹配一次。而preg_match_all全部匹配,直到字串結...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.