我正在尋找使用PHP從錨鏈接中提取圖像href值。請參見下面的說明:使用PHP從錨鏈接中提取圖像href值
開始:
<a href="http://someimagelink.com/image34.jpg">image34.jpg</a>
目標:
http://someimagelink.com/image34.jpg
更具體地說,我怎麼去掉的<a href="
和">image34.jpg</a>
所以它的工作原理與給定的任何圖像?
我正在尋找使用PHP從錨鏈接中提取圖像href值。請參見下面的說明:使用PHP從錨鏈接中提取圖像href值
開始:
<a href="http://someimagelink.com/image34.jpg">image34.jpg</a>
目標:
http://someimagelink.com/image34.jpg
更具體地說,我怎麼去掉的<a href="
和">image34.jpg</a>
所以它的工作原理與給定的任何圖像?
你應該看看PHP的DOM解析器:http://php.net/manual/en/domdocument.loadhtml.php
您也可以使用Simple HTML DOM Parser這樣的:
$html = file_get_html('http://www.google.com/');
// Find all images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
約做這樣的事情是什麼?我不確定這是否是一個好的解決方案或不...思想?
$html = "<a href='http://www.imagelink.com/images33.jpg'>images33.jpg</a>";
preg_match_all('/href=[\'"]?([^\s\>\'"]*)[\'"\>]/', $html, $matches);
$hrefs = ($matches[1] ? $matches[1] : false);
$url = implode('',$hrefs);
echo "<a href='$url'><img src='$url' width='100'></a>" ;
您可以使用PHP DOMDocument來獲取所需內容。代碼看起來像這樣:
$dom_doc = DOMDocument::loadHTML($html_string);
$a_tag_node_list = $dom_doc->getElementsByTagName('a');
foreach($a_tag_node_list as $a_tag_node) {
echo $a_tag_node->attributes->getNamedItem("href")->nodeValue;
}
因此,你正試圖解析一個傳遞的HTML文件? –
是的,這是正確的 – JCHASE11