preg_match() 返回 pattern 所匹配的次数。要么是 0 次(没有匹配)或 1 次,因为 preg_match() 在第一次匹配之后将停止搜索。preg_match_all() 则相反,会一直搜索到 subject 的结尾处。如果出错 preg_match() 返回 FALSE。

解决方案 »

  1.   

    <?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";
    }
    ?>
      

  2.   

    <?php
    $str="<table>
    <tr>·<a target='_blank' href='111' title=''>a</a></td>
    </tr>
    <tr>
    <td>·<a target='_blank' href='222' title=''>b</a></td>
    </tr>
    <tr>
    <td>·<a target='_blank' href='333' title=''>c</a></td>
    </tr>
    </table>";preg_match_all("/<a.*?href=\'(.*?)\'/",$str,$m);
    print_r($m[1]);
    ?>