繁体   English   中英

使用PHP从锚链接中提取图像href值

[英]Extract the image href value from an anchor link using PHP

我期待使用PHP从锚链接中提取图像href值。 见下图:

开始:

<a href="http://someimagelink.com/image34.jpg">image34.jpg</a>

目标:

http://someimagelink.com/image34.jpg

更具体地说,我如何">image34.jpg</a> <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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM