<form id="fbxrn"><thead id="fbxrn"></thead></form>

      當前位置:首頁 > 學無止境 > PHP>正文

      php函數preg_match_all用法總結

      2020-12-09 12:09:06 分類: PHP 閱讀(1988) 編輯:小郭 評論(6)

      preg_match_all -- 進行全局正則表達式匹配


      說明


      int preg_match_all ( string pattern, string subject, array matches [, int flags] )


      在 subject 中搜索所有與 pattern 給出的正則表達式匹配的內容并將結果以 flags 指定的順序放到 matches 中。 


      搜索到第一個匹配項之后,接下來的搜索從上一個匹配項末尾開始。 


      flags 可以是下列標記的組合(注意把 PREG_PATTERN_ORDER 和 PREG_SET_ORDER 合起來用沒有意義): 


      PREG_PATTERN_ORDER


      對結果排序使 $matches[0] 為全部模式匹配的數組,$matches[1] 為第一個括號中的子模式所匹配的字符串組成的數組,以此類推。 


      例子:

      <?php

      preg_match_all ("|<[^>]+>(.*)</[^>]+>|U","<b>example: </b><div align=left>this is a test</div>",$out, PREG_PATTERN_ORDER);

      print $out[0][0].", ".$out[0][1]."\n";

      print $out[1][0].", ".$out[1][1]."\n";

      ?> 


      本例將輸出: 

      <b>example: </b>, <div align=left>this is a test</div>

      example: , this is a test


      因此,$out[0] 包含匹配整個模式的字符串,$out[1] 包含一對 HTML 標記之間的字符串。 


      PREG_SET_ORDER


      對結果排序使 $matches[0] 為第一組匹配項的數組,$matches[1] 為第二組匹配項的數組,以此類推。 


      例子:

      <?php

      preg_match_all ("|<[^>]+>(.*)</[^>]+>|U","<b>example: </b><div align=left>this is a test</div>",$out, PREG_SET_ORDER);

      print $out[0][0].", ".$out[0][1]."\n";

      print $out[1][0].", ".$out[1][1]."\n";

      ?> 


      本例將輸出: 

      <b>example: </b>, example:

      <div align=left>this is a test</div>, this is a test


      本例中,$matches[0] 是第一組匹配結果,$matches[0][0] 包含匹配整個模式的文本,$matches[0][1] 包含匹配第一個子模式的文本,以此類推。同樣,$matches[1] 是第二組匹配結果,等等。 


      PREG_OFFSET_CAPTURE


      如果設定本標記,對每個出現的匹配結果也同時返回其附屬的字符串偏移量。注意這改變了返回的數組的值,使其中的每個單元也是一個數組,其中第一項為匹配字符串,第二項為其在 subject 中的偏移量。本標記自 PHP 4.3.0 起可用。 


      如果沒有給出標記,則假定為 PREG_PATTERN_ORDER。 


      返回整個模式匹配的次數(可能為零),如果出錯返回 FALSE。 


      例子 1. 從某文本中取得所有的電話號碼:

      <?php

      preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",

      "Call 555-1212 or 1-800-555-1212", $phones);

      ?> 


      例子 2. 搜索匹配的 HTML 標記(greedy):

      <?php

      // \\2 是一個逆向引用的例子,其在 PCRE 中的含義是

      // 必須匹配正則表達式本身中第二組括號內的內容,本例中

      // 就是 ([\w]+)。因為字符串在雙引號中,所以需要

      // 多加一個反斜線。

      $html = "<b>bold text</b><a href=howdy.html>click me</a>";

      preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);

      for ($i=0; $i< count($matches[0]); $i++) {

      echo "matched: ".$matches[0][$i]."\n";

      echo "part 1: ".$matches[1][$i]."\n";

      echo "part 2: ".$matches[3][$i]."\n";

      echo "part 3: ".$matches[4][$i]."\n\n";

      }

      ?> 


      本例將輸出: 

      matched: <b>bold text</b>

      part 1: <b>

      part 2: bold text

      part 3: </b>

      matched: <a href=howdy.html>click me</a>

      part 1: <a href=howdy.html>

      part 2: click me

      part 3: </a>


      以上就是php函數preg_match_all用法總結的全部內容了,希望對正在學習PHP的伙伴們有所幫助,更多網站建設教程敬請關注小郭博客。

      標簽: PHP教程

      熱門標簽 更多

      高清性欧美暴力猛交BD_东北偷拍厕所XXXXX_国产+成+人+亚洲欧洲