6

how can I extract the post image from this link using php?

I read that I can't do it with regex.

http://www.huffingtonpost.it/2013/07/03/stupri-piazza-tahrir-durante-proteste-anti-morsi_n_3538921.html?utm_hp_ref=italy

Thank you so much.

michele
  • 26,348
  • 30
  • 111
  • 168

3 Answers3

4
$content=file_get_contents($url);
if (preg_match("/<img.*src=\"(.*)\".*class=\".*pinit\".*>/", $content, $matches)) 
{
echo "Match was found <br />";
echo $matches[0];
}

$matches[0] will print the whole image tag. And if you want to extract only the URL, then you can use $matches[1] to get the same :)

Nidhin Joseph
  • 1,461
  • 3
  • 15
  • 32
  • I'm trying to do the same for "http://techcrunch.com/2014/05/09/facebook-is-down-for-many/" but it doesn't return anything. I know the lies here : but even after few changes it doesn't return anything. Any help would be nice _/\_ – Saurabh Rana May 10 '14 at 07:39
  • That regex was very specific for the pattern in that particular web page. Try this. if (preg_match("/"; echo $matches[0]; } Working : The regex will go in search for a src attribute within the image tag, then extracts the image URL that is assumed to be within double quotes. You can modify it as of your requirements. – Nidhin Joseph May 13 '14 at 04:54
2

You can/must parse your html with DOM, here is example with your case:

$curlResource = curl_init('http://www.huffingtonpost.it/2013/07/03/stupri-piazza-tahrir-durante-proteste-anti-morsi_n_3538921.html?utm_hp_ref=italy');
curl_setopt($curlResource, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlResource, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curlResource, CURLOPT_AUTOREFERER, true);

$page = curl_exec($curlResource);
curl_close($curlResource);


$domDocument = new DOMDocument();
$domDocument->loadHTML($page);

$xpath = new DOMXPath($domDocument);

$urlXpath = $xpath->query("//img[@id='img_caption_3538921']/@src");

$url = $urlXpath->item(0)->nodeValue;

echo $url;

Take your time and learn a little DOM and XPATH it's worth it.

Aurimas Ličkus
  • 9,886
  • 4
  • 24
  • 26
1

Try This ...

$content=file_get_contents($url);
if (preg_match("/src=[\"\'][^\'\']+[\"\']/", $content, $matches)) 
{
    echo "Match was found <br />";
    echo $matches[0];
}
Krishna
  • 381
  • 2
  • 9