0

我有以下html:

<div class="data">
    <p class="info">output</p>
     <p class="info">output2</p>

我正在尝试使用以下内容获取文本“输出”

$xpath = new DOMXPath($dom);    

foreach ($xpath->evaluate("//div[@class='data']/p[1]") as $elt) {
    $output=$elt->wholeText;
}

echo $output;

它不工作。我收到 2 个错误:

Message: Undefined property: DOMElement::$wholeText
Message: Undefined variable: output

谁能指出我正确的方向?

提前致谢,

账单

4

1 回答 1

1

试试这个:

$xpath = new DOMXPath($dom); 

$elements = $xpath->query("*/div[@class='data']");

if (!is_null($elements)) {
  foreach ($elements as $element) {
    $nodes = $element->childNodes;
    foreach ($nodes as $node) {
      echo $node->nodeValue. "\n";
    }
  }
}
于 2013-03-05T21:43:51.803 回答