php中preg_match_all函數用法詳解

來源:互聯網
上載者:User

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


在 subject 中搜尋所有與 pattern 給出的Regex匹配的內容並將結果以 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>, this is a test example: , this is a test

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


對結果排序使 $matches[0] 為全部模式比對的數組,$matches[1] 為第一個括弧中的子模式所匹配的字串組成的數組,以此類推。(即$matches[0] [0]為全部模式比對中的每一項,$matches[0] [1]為全部模式比對中的第二項,$matches[1] [0]為匹配每一個括弧中的第一項,$matches[1] [0]為匹配每一個括弧中的第二項)

 代碼如下 複製代碼

<?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] 為第二組匹配項的數組,以此類推。(即$matches[0] [0]為第一組匹配項中完整匹配的字串,$matches[0] [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)? )? (?(1) [-s] ) d-d/x",

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

 
  ?>

例子 2. 搜尋匹配的 HTML 標籤(greedy)

 代碼如下 複製代碼

 
  <?php

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

 
  // 必須匹配Regex本身中第二組括弧內的內容,本例中

 
  // 就是 ([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]."nn";

 
  }

 
  ?>

本例將輸出:

 
  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>

例1. 在文本中搜尋“php”

 代碼如下 複製代碼

    <?php

    // 模式定界符後面de “i” 表示不區分大小寫字母de搜尋

    if (preg_match (“/php/i”, “PHP is the web scripting language of choice.”)) {

    print “A match was found.”;

    } else {

    print “A match was not found.”;

    }

    ?>

    例2. 搜尋單詞“web”

 代碼如下 複製代碼

    <?php

    /* 模式中de b 表示單詞de邊界,因此只you獨立de “web” 單詞會被匹配,

    * 而不會匹配例如 “webbing” 或 “cobweb” 中de一部分 */

    if (preg_match (“/bwebb/i”, “PHP is the web scripting language of choice.”)) {

    print “A match was found.”;

    } else {

    print “A match was not found.”;

    }

    if (preg_match (“/bwebb/i”, “PHP is the website scripting language of choice.”)) {

    print “A match was found.”;

    } else {

    print “A match was not found.”;

    }

    ?>

    例3. 從 URL 中取出網域名稱

 代碼如下 複製代碼

    <?php

    // 從 URL 中取得主機名稱

    preg_match(“/^(http://)?([^/]+)/i”,

    $host = $matches.;

    // 從主機名稱中取得後面兩段

    preg_match(“/[^./]+.[^./]+$/”, $host, $matches);

    echo “domain name is: {$matches[0]}n”;

    ?>

    輸出:

    domain name is: php.net


preg_match_all 導致apache 重啟的解決辦法

如 preg_match_all("/ni(.*?)wo/", $html, $matches);)進行分析匹配比較長的字串 $html 時(大於10萬位元組,一般用於分析採集回來的網頁源碼),Apache伺服器會崩潰自動重啟。

在Apache錯誤記錄檔裡有這樣的提示:

[Thu Apr 11 18:31:31 2013] [notice] Parent: child process exited with status 128 -- Restarting.
[Thu Apr 11 18:31:31 2013] [notice] Apache/2.2.9 (Win32) PHP/5.2.17 configured -- resuming normal operations
[Thu Apr 11 18:31:31 2013] [notice] Server built: Jun 13 2008 04:04:59
[Thu Apr 11 18:31:31 2013] [notice] Parent: Created child process 2964
[Thu Apr 11 18:31:31 2013] [notice] Disabled use of AcceptEx() WinSock2 API
[Thu Apr 11 18:31:31 2013] [notice] Child 2964: Child process is running
[Thu Apr 11 18:31:31 2013] [notice] Child 2964: Acquired the start mutex.
[Thu Apr 11 18:31:31 2013] [notice] Child 2964: Starting 350 worker threads.
[Thu Apr 11 18:31:31 2013] [notice] Child 2964: Listening on port 80.

那麼如何增加win平台下 ThreadStackSize 的大小呢? 在apache的設定檔 httpd.conf 裡啟用 “Include conf/extra/httpd-mpm.conf”(刪除前面的注釋#),然後在 httpd-mpm.conf 檔案裡的 mpm_winnt_module 配置模組裡設定 “ThreadStackSize 8400000”即可(大約8M)。

 代碼如下 複製代碼

 代碼如下 複製代碼
<IfModule mpm_winnt_module>
    ThreadStackSize 8400000
    ThreadsPerChild      200
    MaxRequestsPerChild    10000
    Win32DisableAcceptEx
</IfModule>

 

這裡需要注意的是,32位的Apache程式只能最多使用大約2GB記憶體空間! 因此,ThreadStackSize 和ThreadsPerChild 的值相乘後(8M * 200)不應該超過2G,否則無法啟動apache,出現的錯誤記錄檔如下:
[Thu Apr 11 20:02:45 2013] [crit] (OS 8)儲存空間不足,無法處理此命令。  : Child 4832: _beginthreadex failed. Unable to create all worker threads. Created 212 of the 220 threads requested with the ThreadsPerChild configuration directive.
    通過上面的提示,飄易可以告訴大家的是在我的這台伺服器上,當線程堆棧大小設為8M時,我可以設定的線程數最多是212個。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.