I want to retrieve the value of href attribute

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
priyanka1985
Forum Newbie
Posts: 2
Joined: Thu Feb 25, 2010 9:43 am

I want to retrieve the value of href attribute

Post by priyanka1985 »

I want to retrieve the value of href tag and then navigate to the page and retrieve some other data.
Please find attached a snippet,

<table>
<tr>
<td></td>
<td></td>
<td></td>
<td><a href="http://www.xyz.com" target="abc"></a></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td><a href="http://www.pqr.com" target="mnp"></a></td>
</tr>
</table>

I am currently using loadHTML() to parse the results for <tr> and <td> tags but i am unable to parse the href attribute of <a> tag. How can we get the value of href attribute i.e. http://www.xyz.com in this case?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: I want to retrieve the value of href attribute

Post by AbraCadaver »

What did you try?

getElementsByTagName()
getAttribute()
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
priyanka1985
Forum Newbie
Posts: 2
Joined: Thu Feb 25, 2010 9:43 am

Re: I want to retrieve the value of href attribute

Post by priyanka1985 »

Hey, I am currently using the following code for parsing,

$tables = $dom->getElementsByTagName("table");
$rows = $tables->item(1)->getElementsByTagName("tr");
foreach($rows as $row){
$cols = $row->getElementsByTagName("td");
echo @$cols->item(0)->nodeValue;
echo @$cols->item(1)->nodeValue;
echo @$cols->item(2)->nodeValue;
echo @$cols->item(3)->nodeValue;
echo "<br />";
}
}

But i am not sure how to incorporate the code for <a> tag. Can you please help?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: I want to retrieve the value of href attribute

Post by AbraCadaver »

Not trying to be rude, but if you use this:

$tables = $dom->getElementsByTagName("table"); to get <table> tags, what do you think you would change to get <a> tags?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply