简体   繁体   中英

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 ?

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

Try

$linkss = $d->getElementsByTagName('a');
$cat = $linkss->item(0)->nodeValue;

or

$linkss = $d->getElementsByTagName('a');
$cat = $linkss->item(0)->textContent;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM