我使用 PHP Simple Html Dom 来获取一些 html,现在我有一个像下面的代码一样的 html dom,我需要获取纯文本内部 div,但避免 p 标签及其内容(仅返回 111111),谁可以帮助我?谢谢提前!
<div>
<p>00000000</p>
111111
<p>22222222</p>
</div>
这取决于“避免 p 标签”的含义。
如果您只想删除标签,那么只需运行strip_tags() https://www.php.net/manual/en/function.strip-tags.php它应该适合你想要的。
如果您实际上只想返回“11111”(即剥离标签及其内容)那么这不是一个可行的解决方案。为此,像这样的事情may work:
$myDiv = $html->find('div'); // wherever your the div you're ending up with is
$children = $myDiv->children; // get an array of children
foreach ($children AS $child) {
$child->outertext = ''; // This removes the element, but MAY NOT remove it from the original $myDiv
}
echo $myDiv->innertext;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)