我希望使用 PHP 从锚链接中提取图像 href 值。见下图:
开始:
<a href="http://someimagelink.com/image34.jpg">image34.jpg</a>
目标:
http://someimagelink.com/image34.jpg
更具体地说,我如何去掉它<a href="
,">image34.jpg</a>
以便它适用于给定的任何图像?
我希望使用 PHP 从锚链接中提取图像 href 值。见下图:
开始:
<a href="http://someimagelink.com/image34.jpg">image34.jpg</a>
目标:
http://someimagelink.com/image34.jpg
更具体地说,我如何去掉它<a href="
,">image34.jpg</a>
以便它适用于给定的任何图像?
你也可以像这样使用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>';
您应该查看 PHP 的 DOM 解析器: http: //php.net/manual/en/domdocument.loadhtml.php
做这样的事情怎么办?我不确定这是否是一个好的解决方案......想法?
$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;
}