2

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

4

1 回答 1

1

这会起作用:

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

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

于 2013-07-11T16:59:37.510 回答