PHP正则表达式多行

时间:2012-02-12 18:54:22

标签: php regex

<table class="trailer">

------------------Begin---------------------
<tbody><tr>
<td class="newtrailer-text">
Trailer 2<br>
</td></tr>
<br>
<b>(Yahoo)</b><br>
<b>(High Definition)</b><br>
<a href="http://playlist.yahoo.com/makeplaylist.dll?sid=107193280&amp;sdm=web&amp;pt=rd">(1080p)</a><br>
<a href="http://playlist.yahoo.com/makeplaylist.dll?sid=107193279&amp;sdm=web&amp;pt=rd">(720p)</a><br>
<a href="http://playlist.yahoo.com/makeplaylist.dll?sid=107193272&amp;sdm=web&amp;pt=rd">(480p)</a><br>
<br>
<b>(Warner Bros.)</b><br>
<b>(High Definition)</b><br>
<a href="http://pdl.warnerbros.com/wbmovies/inception/trl_3/Inception_TRLR3_1080.mov">(1080p)</a><br>
<a href="http://pdl.warnerbros.com/wbmovies/inception/trl_3/Inception_TRLR3_720.mov">(720p)</a><br>
<a href="http://pdl.warnerbros.com/wbmovies/inception/trl_3/Inception_TRLR3_480.mov">(480p)</a>=
--------------END----------------

</tbody></table>

我如何获得开始和结束之间的所有数据? 我试过以下没有结果。任何帮助,将不胜感激。感谢。

$regex = '#<td class="newtrailer-text">([^"]+)</tbody></table>#si';

3 个答案:

答案 0 :(得分:2)

这是the canonical link for why you should use DOM to parse (X)HTML小马,他来了。

但这是你的正则表达式的交易:

([^"]+)只会匹配第一次出现双引号"的所有内容。您的正则表达式指定第一个双引号必须紧接在</tbody>标记之前,否则将找不到匹配项。

相反,请尝试:

$regex = '#<td class="newtrailer-text">(.+)</tbody></table>#siU';

if (preg_match($regex, $str, $m)) {
  echo $m[1];
} else {
  echo 'No match';
}

答案 1 :(得分:2)

$regex = '#<td class="newtrailer-text">(.+)</tbody></table>#Usi';

答案 2 :(得分:1)

您可以使用非贪婪的RegEx:

if (preg_match_all('#------------------Begin---------------------(.*?)--------------END----------------#s', $str, $m) )
   print_r ( $m[1] );