开发者

DOM/XPATH in PHP: What is nodeValue?

开发者 https://www.devze.com 2022-12-11 03:29 出处:网络
Can someone explain to me what is nodeValue in this and write out how nodeValue look like or write out what\'s in nodeValue? EDIT: Sorry! Yes, this is PHP.

Can someone explain to me what is nodeValue in this and write out how nodeValue look like or write out what's in nodeValue? EDIT: Sorry! Yes, this is PHP.

And..

foreach ($elements as $e) {
  echo $e->nodeValue;
}

What does the arrow thingy mean (->)? That's an array right? Well if you can explain to me this one part that would be great...

Here's the source:

$html = file_get_contents('http://website.com/');

$dom = new DOMDocument();

@$dom->loadHTML($html);

$xPath = new DOMXPath($dom);


$elements = $xPath->query("//*[@id='announcement']");


foreach ($elements as $e) {
  echo $e->nodeValue;
}

Update: I thought I'll开发者_开发知识库 write out the question here instead of leaving it in comments. Let's say I had 5 node values found and what if I just wanted to echo the 2nd node value? How would I do that? echo $e->nodeValue2;?


echo $e->items[1]->nodeValue

Arrays in PHP start at 0, so the element at position 1 is the second value in the array.


That looks suspiciously like PHP. I believe that it is getting the contents of the web page from http://website.com, loading it into a DOM Document (Document Object Model), and then performing an XPath query on it which looks for all the nodes which have an 'id' attribute that have a value of 'announcement', and then for each of those nodes, echoing the value.

The node value would be: <node>This is the node value</node>

-> is how you access a class member like a method or property in PHP.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号