我需要一个 php 中的正则表达式来匹配元素标签之间的内容,例如<body>
与</body>
perl 兼容preg_match
。
到目前为止,我尝试过:
// $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);
如文档中所述:http: //nl2.php.net/manual/en/reference.pcre.pattern.modifiers.php
perl 正则表达式默认匹配一行
您必须通过在最后一个 / 之后添加 as 或 am 来指定要进行多行搜索
前任:
$> perl -e 'print $1 if "bla\nbla\n<body>\nfirst line\n second line\n</body>\nbla" =~ /^.*<body>(.*)<\/body>.*$/s'