PHP XPath,无法在目标标记内的标记之后检索文本

时间:2013-07-11 16:40:16

标签: php xpath

我有这个HTML代码,在其上做xpath:

<b>Random Field:</b>
<p>
   A random field describes an <a href="/index.php?page=glossary&term_id=230">
   experiment</a> with outcomes being functions of more than one continuous variable, 
   for example U(x,y,z), where  x, y, and z are coordinates in space. Random field is 
   extension of the concept of <a href="/index.php?page=glossary&term_id=598">random 
   process</a> into the case of multivariate argument.
</p>

我尝试将此文本放在<p>标记内:

$dom = new DomDocument();
$dom->loadHtml($curl_scraped_page);
$xpath = new DomXPath($dom);
print $xpath->evaluate('string(//p[preceding::b]/text())');

但它只是给了我这个:

A random field describes an

我想要的是:

A random field describes an ..(an so on until).. of multivariate argument. 所以我猜测问题出在<a>标签上。因为每次我尝试在同一个图案文档上执行此操作时,它都会在此<a>标记之前停止。 感谢..

1 个答案:

答案 0 :(得分:1)

这样可行:

$xpath->query('//p[preceding::b]')->item(0)->textContent;

XPath中有一个string-join函数,但遗憾的是在PHP使用的lbxml的XPath 1.0版本中没有。