我有一些网址,这些网址的所有html在其标记中都有以下标记
<link rel="image_src" href="http://imgv2-4.scribdassets.com/img/word_document/15490455
/164x212/8a4ab0c34b/1337732662" />
我正在使用以下代码
$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;
//}
}
现在我想抓住这个链接href,但是怎么样,我不知道。请帮帮我
由于
答案 0 :(得分:2)
我更喜欢使用PHP的DOMDocument浏览HTML,而不是preg_match。这样的事情应该有效:
$xpath = new DOMXPath($result);
$links = $xpath->query('//link[@rel="image_src"]');
foreach ($links as $link) {
$src = $link->nodeValue;
}
答案 1 :(得分:2)
这是帮助我的另一种选择。它类似于@Mark Roach的DOMXPATH
建议
$dom = new DOMDocument;
$dom->loadHTML($html);
$nodes = $dom->getElementsByTagName('link');
foreach ($nodes as $node){
if ($node->getAttribute('rel') === 'image_src')
{
echo($node->getAttribute('href'));
}
}
答案 2 :(得分:0)
喜欢这样
<?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;
}
?>