我使用 PHP Simple Html Dom 来获取一些 html,现在我有一个 html dom 就像下面的代码,我需要获取纯文本内部 div,但要避免 p 标签及其内容(只返回 111111),谁能帮忙我?提前致谢!
<div>
<p>00000000</p>
111111
<p>22222222</p>
</div>
最佳答案
这取决于你所说的“避免 p 标签”是什么意思。
如果你只想删除标签,那么只需运行strip_tags()
它应该可以满足您的需求。
如果您真的只想返回“11111”(即去除标签及其内容),那么这不是一个可行的解决方案。为此,像这样的东西可能起作用:
$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;
关于PHP Simple Html Dom获取div的纯文本,但避开所有其他标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25662569/