一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php中preg_match_all函数用法详解

时间:2013-06-05 编辑:简简单单 来源:一聚教程网

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] 为第一个括号中的子模式所匹配的字符串组成的数组,以此类推。

 代码如下 复制代码

preg_match_all ("|<[^>]+>(.*)]+>|U",
    "example:

this is a test
",
    $out, PREG_PATTERN_ORDER);
print $out[0][0].", ".$out[0][1]."/n";
print $out[1][0].", ".$out[1][1]."/n";
?>

本例将输出:

example: , 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]为匹配每一个括号中的第二项)

 代码如下 复制代码

 
  preg_match_all ("|<[^>]+>(.*)]+>|U",

 
  "example:

this is a test
",

 
  $out, PREG_PATTERN_ORDER);

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

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

 
  ?>

 


本例将输出:

 
  example: ,

this is a test

 
  example: , this is a test

 
因此,$out[0] 包含匹配整个模式的字符串,$out[1] 包含一对 HTML 标记之间的字符串。

 
如果使用PREG_SET_ORDER


对结果排序使 $matches[0] 为第一组匹配项的数组,$matches[1] 为第二组匹配项的数组,以此类推。(即$matches[0] [0]为第一组匹配项中完整匹配的字符串,$matches[0] [1]为第一组匹配中完整匹配第一个括号中的字符串)

 代码如下 复制代码

 
  preg_match_all ("|<[^>]+>(.*)]+>|U",

 
  "example:

this is a test
",

 
  $out, PREG_SET_ORDER);

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

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

 
  ?>

 


本例将输出:

 
  example: , example:

 
  

this is a test
, 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. 从某文本中取得所有的电话号码

 代码如下 复制代码

 
  

 
  preg_match_all ("/(? (d)? )? (?(1) [-s] ) d-d/x",

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

 
  ?>

例子 2. 搜索匹配的 HTML 标记(greedy)

 代码如下 复制代码

 
  

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

 
  // 必须匹配正则表达式本身中第二组括号内的内容,本例中

 
  // 就是 ([w]+)。因为字符串在双引号中,所以需要

 
  // 多加一个反斜线。

 
  $html = "bold textclick me";

 
  preg_match_all ("/(<([w]+)[^>]*>)(.*)()/", $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: bold text

 
  part 1:

 
  part 2: bold text

 
  part 3:

 
  matched: click me

 
  part 1:

 
  part 2: click me

 
part 3:

例1. 在文本中搜索“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”

 代码如下 复制代码

   

    /* 模式中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 中取出域名

 代码如下 复制代码

   

    // 从 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)。

 代码如下 复制代码

 代码如下 复制代码

    ThreadStackSize 8400000
    ThreadsPerChild      200
    MaxRequestsPerChild    10000
    Win32DisableAcceptEx

 

这里需要注意的是,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个。

热门栏目