我有这个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>标记之前停止。
谢谢..

最佳答案

这将工作:

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


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

关于php - PHP XPath,无法在目标标记内的标记后检索文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17598942/

10-16 14:57