本文介绍了匹配html< body>之间的所有内容使用PHP的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,该脚本在名为$ content的变量中返回以下内容

I have a script that returns the following in a variable called $content

<body>
<p><span class=\"c-sc\">dgdfgdf</span></p>
</body>

但是我需要将body标签之间的所有内容放置在称为matchs的数组内

I however need to place everything between the body tag inside an array called matches

我执行以下操作以匹配正文标签之间的内容

I do the following to match the stuff between the body tag

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

但是$ mathces数组为空,如何获取它返回body标记内的所有内容

but the $mathces array is empty, how could I get it to return everything inside the body tag

推荐答案

您不应使用正则表达式来解析HTML.

You should not use regular expressions to parse HTML.

在这种情况下,您的特殊问题是您需要添加 DOTALL修饰符,以便点与换行符匹配.

Your particular problem in this case is you need to add the DOTALL modifier so that the dot matches newlines.

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

但是请认真使用HTML解析器.上面的正则表达式可能有很多种破译的方式.

But seriously, use an HTML parser instead. There are so many ways that the above regular expression can break.

这篇关于匹配html&lt; body&gt;之间的所有内容使用PHP的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 21:59