PHP Simple Html Dom获取div的纯文本,但避免使用所有其他标记

Jac*_*Sun 4 php simple-html-dom

我使用PHP Simple Html Dom来获取一些HTML,现在我有一个像跟随代码的html dom,我需要获取纯文本内部div,但避免使用p标签及其内容(仅返回111111),谁可以帮助我?谢谢提前!

<div>
    <p>00000000</p>
    111111
    <p>22222222</p>
</div>
Run Code Online (Sandbox Code Playgroud)

Joe*_*Joe 10

这取决于你的意思是"避免使用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;
Run Code Online (Sandbox Code Playgroud)


Eni*_*say 7

如果文本始终位于相同位置,请尝试以下操作:

$html->find('text', 2)->plaintext; // should return 111111
Run Code Online (Sandbox Code Playgroud)