<img>
的src
已知,以及目标网页的网址,如http://test.com/directory/hi.html
,如何实现一般函数来检索图像的绝对url?如何在PHP中获取<img />的绝对网址?
-2
A
回答
6
我想:
- 检查是否IMG URL开始用主机的价值,如果是的话,返回它,因为它已经是绝对的
- 解析网页的URL获得基地主机的价值
- 检查img src是否相对于根目录或目录,并根据需要附加到基本主机值。
parse_url()将是选择的功能在这里。我感到勇敢,所以我实现它你:
function getAbsoluteImageUrl($pageUrl,$imgSrc)
{
$imgInfo = parse_url($imgSrc);
if (! empty($imgInfo['host'])) {
//img src is already an absolute URL
return $imgSrc;
}
else {
$urlInfo = parse_url($pageUrl);
$base = $urlInfo['scheme'].'//'.$urlInfo['host'];
if (substr($imgSrc,0,1) == '/') {
//img src is relative from the root URL
return $base . $imgSrc;
}
else {
//img src is relative from the current directory
return
$base
. substr($urlInfo['path'],0,strrpos($urlInfo['path'],'/'))
. '/' . $imgSrc;
}
}
}
//tests
$host = 'http://test.com/directory/hi.html';
$imgSrc = '/images/lolcat.jpg';
echo getAbsoluteImageUrl($host,$imgSrc);
//echos http//test.com/images/lolcat.jpg
$host = 'http://test.com/directory/hi.html';
$imgSrc = 'images/lolcat.jpg';
echo getAbsoluteImageUrl($host,$imgSrc);
//echos http//test.com/directory/images/lolcat.jpg
$host = 'http://test.com/directory/hi.html';
$imgSrc = 'http://images.com/lolcat.jpg';
echo getAbsoluteImageUrl($host,$imgSrc);
//echos http://images.com/lolcat.jpg
-1
如果我没有理解这个问题很好,你可以定义如下的应用程序根目录的APPLICATION_PATH事后使用它:
define('APPLICATION_PATH', realpath(dirname(__FILE__)));
...
<img src="<?php APPLICATION_PATH ?>/path/to/image.png" alt="" />
...
0
使用parse_url,你可以做这样的事情:
- 解析页面的URL到它的部分(方案,领域,路径,文件,查询,片段)
- 重建img url的url,具体取决于img url的方式。
- 图网址开头是“/”,这是一个绝对URL,所以我们建立的图像网址像
"$scheme://$domain$img_url"
- 图网址没有以“/”开头,所以它是相对的,所以我们所建立的图像网址像
"$scheme://$domain/$path/$img_url"
。如果img_url是“.././"-style”,这也是有效的,因为那些将被服务器解析。
- 图网址开头是“/”,这是一个绝对URL,所以我们建立的图像网址像
您可能还需要排除与方案或域启动IMG的url( 'XX://' 或 'XX.XX /')。
0
接受的答案没有考虑以//开头的src。这使得该计划根据原点而开放式结束。我认为他们的答案中还有一个遗漏的冒号。这是我的稍微修改版本,似乎工作正常。注意它使用PHP7语法。
function getAbsoluteImageUrl($pageUrl,$imgSrc)
{
$imgInfo = parse_url($imgSrc);
var_dump(parse_url($imgSrc));
if (!empty($imgInfo['host'])) {
//img src is already an absolute URL
if (substr($imgSrc,0,2) == '//') {
return (parse_url($pageUrl)['scheme'] ?? "http") . ":" . $imgSrc;
} else {
return $imgSrc;
}
}
else {
$urlInfo = parse_url($pageUrl);
$base = $urlInfo['scheme'].'://'.$urlInfo['host'];
if (substr($imgSrc,0,1) == '/') {
//img src is relative from the root URL
return $base . $imgSrc;
}
else {
//img src is relative from the current directory
return
$base
. substr($urlInfo['path'],0,strrpos($urlInfo['path'],'/'))
. '/' . $imgSrc;
}
}
}
相关问题
- 1. 在绝对网址和相对网址的javascript中获取
- 2. 如何从xpath获取绝对网址?
- 3. 如何从网页的HTML中获取绝对网址
- 4. 放置img没有绝对<css>
- 5. BeautifulSoup如何从.. img src获取网址../ ..?
- 6. html/php:如何处理绝对网址?
- 7. 我该如何中心<img /><h1>...</h1><img /> HTML + CSS?
- 8. 如何在RSS中从CDATA获取<img> src?
- 9. 获取SharePoint绝对站点网址
- 10. 使用路由获取绝对网址
- 11. 如何获取绝对路由URL作为字符串在Play框架2?</p> <pre><code>String url = controllers.directory.routes.Directory.viewOrganisation(org.id).url(); </code></pre> <p>这工作得很好,但是我想获得完整的绝对网址:
- 12. Magnific-popup:如何从<img src="...">得到图片网址?
- 13. 如何获取<ice:graphicImage>元素的网址?
- 14. 从相关网址获取绝对网址。 (IE6的问题)
- 15. 设置图像大小<img></img>,从数据库中获取
- 16. 获取所有<img>元素在网页上
- 17. 你如何使用jquery获取图像的绝对网址
- 18. 在Angular-UI中获取状态的“绝对”网址?
- 19. 如何从xml文件中获取<img> src值?
- 20. 使用.children()获取<img>元素
- 21. .attr()获取<img>内容
- 22. 如何从ArrayList中获取对象<HashMap <String,String >>
- 23. xhtml在<img .... />和<img ...>之间的区别</img>?
- 24. 如何在iframe中定位<img>?
- 25. 正则表达式将<img>相对URL改为绝对
- 26. 如何获得<g>组内对象的绝对坐标?
- 27. 如何为我的网站使用php创建绝对网址?
- 28. PHP - 获取<br>和<br>
- 29. 如何获取Foo <?>对象的<?>值?
- 30. z-index> css background-img>绝对img> p标记
这是假设它是本地图像。我认为OP需要一个URL。 – 2009-12-29 02:05:19
这不是我想要的... – user198729 2009-12-29 02:08:42
需要更多的说明,绝对url?!你的意思是基地网址?一个例子可能有帮助! – sepehr 2009-12-29 02:10:29