I have some url and all the html of these urls have the following tag in their tag
<link rel="image_src" href="http://imgv2-4.scribdassets.com/img/word_document/15490455
/164x212/8a4ab0c34b/1337732662" />
I am using the following code
$url = 'my url';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // The url to get links from
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // We want to get the respone
$result = curl_exec($ch);
$regex='|<a.*?href="(.*?)"|';
preg_match_all($regex,$result,$parts);
$links=$parts[1];
foreach($links as $link){
//if(strpos($link,'format=json') !==false) {
echo $link;
//}
}
Now I want to grab this link href but how, I don't know. Please help me
Thanks
Like so
<?php
$url = 'http://www.scribd.com/doc/15490455/Learning-PHP-5';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // The url to get links from
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // We want to get the respone
$result = curl_exec($ch);
$regex='#.*link rel=\"image_src\" href=\"(.*)\"./>#';
preg_match($regex,$result,$parts);
foreach ($parts as $part) {
echo = $part;
}
?>
I prefer using PHP's DOMDocument going through HTML, versus preg_match. Something like this should work:
$xpath = new DOMXPath($result);
$links = $xpath->query('//link[@rel="image_src"]');
foreach ($links as $link) {
$src = $link->nodeValue;
}
Here's another alternative that helped me. It's similar to the DOMXPATH
suggestion by @Mark Roach
$dom = new DOMDocument;
$dom->loadHTML($html);
$nodes = $dom->getElementsByTagName('link');
foreach ($nodes as $node){
if ($node->getAttribute('rel') === 'image_src')
{
echo($node->getAttribute('href'));
}
}