Php Xpath选择文本
我一直在玩xpath,当我选择段落时,我可以让它工作,但是这篇文章似乎不起作用 这是htmlPhp Xpath选择文本,php,html,xml,xpath,Php,Html,Xml,Xpath,我一直在玩xpath,当我选择段落时,我可以让它工作,但是这篇文章似乎不起作用 这是html <span id="favorite_count" style="display: block;"> <span style="cursor:help; border-bottom: 1px dotted black;" title="Active members who have made you their favorite. This number may change
<span id="favorite_count" style="display: block;">
<span style="cursor:help; border-bottom: 1px dotted black;" title="Active members who have made you their favorite. This number may change as new members join, or close their accounts.">My total number of <span class="favorites">:</span>
</span>
19458
</span>
注:
我知道这与上面这条线有关,因为当我使用
$favorites = $data->xpath( '//span[@id="favorite_count"]/span' );
我得到的结果,我的总数
此外,我无法更改HTML,因为它来自我无权修改的页面。HTML代码:
<html>
<head></head>
<body>
<span id="favorite_count" style="display: block;">
<span style="cursor:help; border-bottom: 1px dotted black;" title="Active members who have made you their favorite. This number may change as new members join, or close their accounts.">My total number of <span class="favorites">:</span>
</span>
19458
</span>
</body>
</html>
PHP代码:
/* Use internal libxml errors -- turn on in production, off for debugging */
libxml_use_internal_errors(true);
/* Createa a new DomDocument object */
$dom = new DomDocument;
/* Load the HTML */
$dom->loadHTMLFile("test.html");
/* Create a new XPath object */
$xpath = new DomXPath($dom);
/* Query all <td> nodes containing specified class name */
$nodes = $xpath->query("//*[@id='favorite_count']/text()");
/* Set HTTP response header to plain text for debugging output */
header("Content-type: text/plain");
/* Traverse the DOMNodeList object to output each DomNode's nodeValue */
foreach ($nodes as $i => $node) {
echo "Node($i): ", $node->nodeValue, "\n";
}
输出:
节点0:
节点1:
19458您正在使用SimpleXML库。它无法使用其方法选择textnodes 要使其正常工作,需要扩展SimpleXMLElement并动态采用结果。在它的姐妹库的帮助下,这很容易实现 示例代码:
echo (new DOMXpath(
dom_import_simplexml(
simplexml_load_string($html)
)->ownerDocument
))->evaluate('normalize-space(//span[@id="favorite_count"]/text()[last()])');
程序输出:
19458
演示:
在您非常具体的情况下,您还可以直接使用SimpleXML执行以下操作:
echo trim($xml->xpath('//span[@id="favorite_count"]')[0]);
这是因为内部确实隐藏了实际的节点值,但它只返回空格、行分隔符和数字19458
相关问题:
echo trim($xml->xpath('//span[@id="favorite_count"]')[0]);