I need to extract the link value which is stored in a <a href>
tag by using php code.
<a href="http://stackoverflow.com/questions/ask"></a>
From the above code i want to extract the link http://stackoverflow.com/questions/ask
using php code.
You can't do that unless the link is generated by your PHP code itself. PHP is server side code whereas the link is on the page itself is in HTML client code. The two can't communicate with each other. To extract the value you would need to use a client side script like JavaScript.
There are a variety of options..
From the limited information provided, I would start with the first or second options and only go to a regular expression or SimpleXML if there were additional requirements in the mix.
If you already have the anchor tag and just want to get the href value, try this code. It will remove other except the 'href' value. The only problem is the word 'href' can't be inside id/class infront of 'href' attribute.
$url = '<a href="http://stackoverflow.com/questions/ask"></a>';
$url = substr ( $url,(strpos($url,' href=') + 6) );
$url = explode($url[0],$url);
echo $url[1]; // $url[1] will store the 'http://stackoverflow.com/questions/ask'
$url = '<a title="Question" href="http://stackoverflow.com/questions/ask"></a>';
preg_match("/href=\"(.*?)\"/i", $url, $matches);
print_r($matches);
/*
Match Group(s) :
1. http://stackoverflow.com/questions/ask
*/