<blockquote>
<p>
2 1/2 cups sweet cherries, pitted<br>
1 tablespoon cornstarch <br>
1/4 cup fine-grain natural cane sugar
</p>
</blockquote>
嗨,我想在’p’标签内得到文字.你看到有三条不同的线,我想在每行添加一些额外的文字后单独打印它们.这是我的代码块
$tags = $dom->getElementsByTagName('blockquote');
foreach($tags as $tag)
{
$datas = $tag->getElementsByTagName('p');
foreach($datas as $data)
{
$line = $data->nodeValue;
echo $line;
}
}
主要问题是$line包含’p’标签内的全文,包括’br’标签.我怎么能分开这三条线来分别对待它们?
提前致谢.
解决方法:
你可以用XPath做到这一点.您所要做的就是查询文本节点.无需爆炸或类似的东西:
$dom = new DOMDocument;
$dom->loadHtml($html);
$xp = new DOMXPath($dom);
foreach ($xp->query('/html/body/blockquote/p/text()') as $textNode) {
echo "\n<li>", trim($textNode->textContent);
}
非XPath替代方法是迭代P标记的子标记,并在它们是DOMText节点时仅输出它们:
$dom = new DOMDocument;
$dom->loadHtml($html);
foreach ($dom->getElementsByTagName('p')->item(0)->childNodes as $pChild) {
if ($pChild->nodeType === XML_TEXT_NODE) {
echo "\n<li>", trim($pChild->textContent);
}
}
<li>2 1/2 cups sweet cherries, pitted
<li>1 tablespoon cornstarch
<li>1/4 cup fine-grain natural cane sugar
有关节点概念的说明,请参见DOMDocument in php.了解何时使用DOM至关重要.