【问题标题】:How to get the hyperlink from an image when given its url给定其网址时如何从图像中获取超链接
【发布时间】:2015-06-16 00:07:02
【问题描述】:

我想知道如果给出图像 url 是否可以获得图像的超链接,例如:我有一个网页说 (http://www.example.com) 带有图像 例如:

<a href="http://example.com/12344/623/2"><img id="img" src="http://example.com/623/5602225.jpg" alt="The Image" name="img" height="1140" width="800"></a>

我在想有没有可能得到这个

http://example.com/12344/623/2

当我给予时

http://example.com/623/5602225.jpg

作为 php 和 CURL 的输入?

【问题讨论】:

  • 在您的问题中,http://example.com/12344/623/2http://example.com/623/5602225.jpg 除了623 之外彼此没有关联。你能说清楚吗?
  • 没关系,如果你用精确的参数编辑问题,它会很容易帮助你
  • 它不像我输入图像 url 即 src 并且我想获取与之关联的 href 属性

标签: php html image image-processing simple-html-dom


【解决方案1】:

是的,你需要解析html。

当您标记 SimpleHtmlDom 时:

include "simple_html_dom.php";
//example html
$html = '<a href="http://example.com/12344/623/1"><img id="img" src="http://example.com/623/5602224.jpg" alt="The Image" name="img" height="1140" width="800"></a>';
$html .= '<a href="http://example.com/12344/623/2"><img id="img" src="http://example.com/623/5602225.jpg" alt="The Image" name="img" height="1140" width="800"></a>';
$html .= '<a href="http://example.com/12344/623/3"><img id="img" src="http://example.com/623/5602226.jpg" alt="The Image" name="img" height="1140" width="800"></a>';

//the image url to search for
$img = 'http://example.com/623/5602225.jpg';

//Note if you want to get the html from a url and not a string,
// use $som = file_get_html('http://www.example.com/');
$dom = str_get_html($html);
$url = $dom->find('img[src='.$img.']', 0)->parent->href;
//$url equals http://example.com/12344/623/2

【讨论】:

    猜你喜欢
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 2016-06-11
    • 2018-03-22
    • 1970-01-01
    • 2022-01-05
    相关资源
    最近更新 更多