我需要一个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);
...但打印输出是一个空数组。
我需要一个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);
...但打印输出是一个空数组。
您只需添加s
修饰符,这样的点匹配所有的字符,包括新行:
preg_match("/<body.*\/body>/s", $content, $matches);
在doc作为解释:http://nl2.php.net/manual/en/reference.pcre.pattern.modifiers.php
默认一行Perl的正则表达式匹配
你必须指定你想要做一个多行搜索,通过添加最后的或最后的/
例如:
$> perl -e 'print $1 if "bla\nbla\n<body>\nfirst line\n second line\n</body>\nbla" =~ /^.*<body>(.*)<\/body>.*$/s'
设置-m标志是不够的,因为它只会改变^和$操作符的行为。 – Wookai 2009-08-12 08:58:16
谢谢,它的工作! – Spoike 2009-08-12 09:46:28