xpath:在break标签之前和之后选择文本节点 [英] xpath: select text nodes before and after break tags

查看:71
本文介绍了xpath:在break标签之前和之后选择文本节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下内容:(<br><br/>的混合物)

considering the following : (mixture of <br> and <br/>)

text1
<br>
text2
<br/>
text3
<br/>
text4
<br>
text5

如何找到每个文本节点?

How can I locate each text nodes ?

我在考虑某种适合br标签之前或之后的条件....但是不确定<br><br/>在xpath中是否被区别对待.

I am thinking something that fits the condition of preceding OR following a br tag....but unsure if <br> and <br/> are treated differently in xpath.

推荐答案

DOMDocument loadHtml()方法在无效的HTML片段中效果很好,因此您可以通过以下方式使用DOMXPath:

DOMDocument's loadHtml() method works well with invalid HTML fragments, so you can use DOMXPath this way:

<?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);
}

这篇关于xpath:在break标签之前和之后选择文本节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆