2009-08-12 51 views
5

我需要一个php的正则表达式来匹配元素的标签之间的内容。 <body></body>与perl兼容preg_match正则表达式来匹配PHP中的HTML正文的内容

到目前为止,我试着用:

// $content is a string with html content 

preg_match("/<body(.|\r\n)*\/body>/", $content, $matches); 

print_r($matches); 

...但打印输出是一个空数组。

回答

0

默认一行Perl的正则表达式匹配

你必须指定你想要做一个多行搜索,通过添加最后的或最后的/

例如:

$> perl -e 'print $1 if "bla\nbla\n<body>\nfirst line\n second line\n</body>\nbla" =~ /^.*<body>(.*)<\/body>.*$/s' 

见: http://www.perl.com/pub/a/2003/06/06/regexps.html

+0

设置-m标志是不够的,因为它只会改变^和$操作符的行为。 – Wookai 2009-08-12 08:58:16