我试图从div中获取文本,其中class =’review-text’,通过使用具有以下
HTML(相同结构)的
PHP的DOM元素和以下代码.
但是这似乎不起作用
> HTML
$html = '
Outstanding ...
';
> PHP代码
$classname = 'review-text';
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$results = $xpath->query("//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
if ($results->length > 0) {
echo $review = $results->item(0)->nodeValue;
}
在Blog提供了按类别选择元素的XPATH语法
我已经尝试过StackOverflow,在线教程的许多例子,但没有一个似乎工作.我错过了什么吗?