如何选择以下节点的字符串内容:
<span class="url">
word
<b class=" ">test</b>
</span>
<span class="url">
word
<b class=" ">test2</b>
more words
</span>
我尝试过一些事情
//span/text()
没有获得粗体标签
//span/string(.)
是无效的
string(//span)
只选择1个节点
我在 php 中使用 simple_xml ,我认为唯一的其他选择是使用 //span ,它返回:
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => url
)
[b] => test
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => url
)
[b] => test2
)
)
*请注意,它还会从第二个跨度中删除“更多单词”文本。
所以我想我可以如何使用 php 压平数组中的项目? Xpath 是首选,但任何其他想法也会有帮助。
$xml = '<foo>
<span class="url">
word
<b class=" ">test</b>
</span>
<span class="url">
word
<b class=" ">test2</b>
more words
</span>
</foo>';
$dom = new DOMDocument();
$dom->loadXML($xml); //or load an HTML document with loadHTML()
$x= new DOMXpath($dom);
foreach($x->query("//span[@class='url']") as $node) echo $node->textContent;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)