5

我需要一个 php 中的正则表达式来匹配元素标签之间的内容,例如<body></body>perl 兼容preg_match

到目前为止,我尝试过:

// $content is a string with html content

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

print_r($matches);

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

4

2 回答 2

9

您只需添加s修饰符即可使点匹配所有字符,包括新行:

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

如文档中所述:http: //nl2.php.net/manual/en/reference.pcre.pattern.modifiers.php

于 2009-08-12T08:55:00.880 回答
0

perl 正则表达式默认匹配一行

您必须通过在最后一个 / 之后添加 as 或 am 来指定要进行多行搜索

前任:

$> 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

于 2009-08-12T08:55:36.573 回答