Rick Joe Rick Joe - 1 month ago 8
PHP Question

get href content using rel value - getElementsByTagName - php

I have in my page one html like this:

<li class=category><a href="#" rel="category name">WHAT I WANT</a></li>
<li class=name><a href="#" rel="name">name</a></li>
<li class=date><a href="#" rel="date">date</a></li>


I need to get the
WHAT I WANT
value.

So I tried:

$linkss = $d->getElementsByTagName('a');
$cat = $linkss->item(0)->getAttribute('category name');


but it is not working... Any ideas why? How can I get just the What I want inside the
href rel category name
?

Answer

The attribute is rel, not category name; category name is the value of the attribute. You need to varify the attribute matches then output the nodeValue when you have a match.

$linkss = $d->getElementsByTagName('a');
foreach($linkss as $link) {
   if($link->getAttribute('rel') == 'category name') {
        echo $link->nodeValue;
   }
}

Demo: https://eval.in/668733

Comments