2013-07-03 29 views

回答

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

$ matches [0]将打印整个图像标签。 如果你想只提取URL,那么你可以使用$匹配[1]与您的情况下获得相同的:)

+0

我试图做同样的“http://techcrunch.com/2014/05/09/facebook-is-down-for-many/”,但它不会返回任何东西。我知道位于这里:但即使经过很少的改变它不会返回任何东西。任何帮助都会很好_/\ _ –

+0

该正则表达式对于特定网页中的模式非常具体。 试试这个。 如果(的preg_match(“/ ”; 回声$比赛[0]; } 工作:正则表达式将在搜索图像标签内的src属性去,然后提取假定图像URL是在双引号内 您可以根据您的要求修改它。 –

1

你可以/必须分析与DOM的HTML,这里是例子:

$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; 

花点时间学习一点DOM和XPATH是值得的。

1

尝试......

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