user2178521
user2178521

Reputation: 843

PHP getElementByTagName nodeValue return nothing

//example page
<h1 id="t">Hello You</h1>


//CURL
$url = 'http://www.example.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$site = curl_exec($ch);

//DOM
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($site);



$byId =  $dom->getElementById('t');
$byName = $dom->getElementsByTagName('h1');

echo $byId->nodeValue;
echo $byName->nodeValue;

I have a page I try to get nodeValue

When I use getElementById, I got the value, but If I tried byTagName, it return nothing.

Upvotes: 0

Views: 51

Answers (1)

Bora
Bora

Reputation: 10717

getElementsByTagName returns object containing all the matched elements.

You should use like following:

$byName = $dom->getElementsByTagName('h1')->item(0);

Upvotes: 2

Related Questions