Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP DOMDocument, require first item only

Hello I have use DOMDocs in the past but I am stuck how to do this.

$xmldoc = new DOMDocument();
$xmldoc->load('http://example.com');
$feeditem = $xmldoc->getElementsByTagName('data');

I only want the first instance of the tag 'data'. Ordinarily I would write a foreach loop:

foreach($feeditem as $element){ //}

and do what i want with each feeditem but here I only want the first $feeditem. $feeditem isn't an array, it's a DOMdoc object so I can't do $feeditem[0] to select the first 'data' tag.

Thank you for any help with this.

like image 811
David Willis Avatar asked Oct 25 '09 15:10

David Willis


2 Answers

If you look up the documentation for getElementsByTagName, you can see that it returns a DOMNodeList. So after reading the documentation for DOMNodeList, you should find out that you can use $feeditem->item(0).

like image 135
Lukáš Lalinský Avatar answered Nov 14 '22 06:11

Lukáš Lalinský


$feeditem->item(0);

(reference)

like image 21
Zed Avatar answered Nov 14 '22 04:11

Zed