考虑以下因素:(混合<br>
and <br/>
)
text1
<br>
text2
<br/>
text3
<br/>
text4
<br>
text5
如何找到每个文本节点?
我正在考虑符合 br 标签之前或之后的条件的东西......但不确定是否<br>
and <br/>
在 xpath 中的处理方式不同。
DOM文档 http://php.net/manual/en/class.domdocument.php's 加载Html()方法适用于无效的 HTML 片段,因此您可以这样使用 DOMXPath:
<?php
$html = 'text1
<br>
text2
<br/>
text3
<br/>
text4
<br>
text5';
echo "<pre>" . htmlentities($html) . "</pre><br>\n";
$dom = new DOMDocument();
// loadHtml() needs mb_convert_encoding() to work well with UTF-8 encoding
$dom->loadHtml(mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8"));
$xpath = new DOMXPath($dom);
echo "Text nodes preceding br:";
foreach($xpath->query('//text()[(following::br)]') as $node)
{
var_dump($node->wholeText);
}
echo "Text nodes following br:";
foreach($xpath->query('//text()[(preceding::br)]') as $node)
{
var_dump($node->wholeText);
}
echo "Text nodes following OR preceding br:";
foreach($xpath->query('//text()[(following::br) or (preceding::br)]') as $node)
{
var_dump($node->wholeText);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)