PHP DomDocument在标记后获取文本 [英] PHP DomDocument get text after tag
本文介绍了PHP DomDocument在标记后获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的php文件中有这个.
I have this in my php file.
<?php
$str = '<div>
<p>Text</p>
I need this text...
<p>next p</p>
... and this
</div>
';
$dom=new DomDocument();
$dom->loadHTML($str);
$p = $dom->getElementsByTagName('p');
foreach ($p as $item) {
echo $item->nodeValue;
}
这为我提供了p标签的正确文本,但我还需要p标签之间的文本(我需要此文本...","...和此").
This gives me the correct text for the p tags, but I also need the the text between the p tags ("I need this text...", "...and this").
有人知道如何在p标记后获取文本吗?
Anyone know how to get the text after the p tag?
最佳
推荐答案
使用DOMXPath:
Use DOMXPath:
$xpath = new DOMXpath($domDocument);
foreach ($xpath->query('//div/text()') as $textNode) {
echo $textNode->nodeValue;
}
这篇关于PHP DomDocument在标记后获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文