PHP: Translate relative img src urls to absolute

空扰寡人 提交于 2020-01-02 09:54:07

问题


I have fetched an HTML page using cURL into a string and loaded it up in a DOMDocument. There I can get all the img tags and their source attributes. My problem now is... how can I make these URLs absolute?

The list of URLs can contain all kinds of variants, for example:

  • foobar.jpg
  • http://example.com/foobar.jpg
  • /foobar.jpg
  • ../foobar.jpg
  • folder/foobar.jpg

If the HTML is fetched from an arbitrary URL, what is a safe way of converting these image URLs into absolute ones? Is there a way you can take the base tag into consideration too?


回答1:


Here is great PHP example how to do this.

function rel2abs($rel, $base) { 
// something
}

More good examples:

  • How to convert a relative URL to an absolute URL
  • How to parse and build URLs



回答2:


Here you are a handy function found on this page :

function absUrl($rel, $base) {
    if (parse_url($rel, PHP_URL_SCHEME) != '') return $rel;
    if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;
    extract(parse_url($base));
    $path = preg_replace('#/[^/]*$#', '', $path);
    if ($rel[0] == '/') $path = '';
    $abs = "$host$path/$rel"; 
    $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
    for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}   
    return $scheme.'://'.$abs;
}

$rel is your relative path and $base is your base URL.



来源:https://stackoverflow.com/questions/5653859/php-translate-relative-img-src-urls-to-absolute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!