PHP: Translate relative img src urls to absolute

匆匆过客 提交于 2019-12-06 11:49:56
Wh1T3h4Ck5

Here is great PHP example how to do this.

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

More good examples:

Alexander Ivanov

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.

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