3

I've a page loaded with DOM, then I want to convert all relative URLs of anchors to absolute URLs according to, eventually, the <base href> tag

I'm looking for something tested, not some random script that fails on some cases

I'm interested in parsing of every form of href="" usage:

href="relative.php"
href="/absolute1.php"
href="./relative.php"
href="../relative.php"
href="//absolutedomain.org"
href="." relative
href=".." relative
href="../" relative
href="./" relative

and more complex ones mixed

thank you in advance

skyline26
  • 1,994
  • 4
  • 23
  • 35
  • 1
    this is dublicate of http://stackoverflow.com/questions/4444475/transfrom-relative-path-into-absolute-url-using-php – qdinar Jan 03 '16 at 15:57

2 Answers2

1
<?php

//Converting relative urls into absolute urls | PHP Tutors

$base_url = 'http://www.xyz.com/ ';
$anchors[0] = '<a href="test1.php" >Testing Link1 </a >';
$anchors[1] = '<a href="test2.php" >Testing Link2 </a >';

foreach($anchors as $val) {
    if(strpos($val,$base_url) === false) {
        echo str_replace('href="','href="'.$base_url,$val)."<br/ >";
    } else {
        echo $val."<br/ >";
    }
}
?>

Reference

Mirco Widmer
  • 2,139
  • 1
  • 20
  • 44
Hassan Amir Khan
  • 645
  • 8
  • 13
1

This function will resolve relative URLs to a given current page url in $pgurl without regex. It successfully resolves:

/home.php?example types,

same-dir nextpage.php types,

../...../.../parentdir types,

full http://example.net urls,

and shorthand //example.net urls

//Current base URL (you can dynamically retrieve from $_SERVER)
$pgurl = 'http://example.com/scripts/php/absurl.php';

function absurl($url) {
 global $pgurl;
 if(strpos($url,'://')) return $url; //already absolute
 if(substr($url,0,2)=='//') return 'http:'.$url; //shorthand scheme
 if($url[0]=='/') return parse_url($pgurl,PHP_URL_SCHEME).'://'.parse_url($pgurl,PHP_URL_HOST).$url; //just add domain
 if(strpos($pgurl,'/',9)===false) $pgurl .= '/'; //add slash to domain if needed
 return substr($pgurl,0,strrpos($pgurl,'/')+1).$url; //for relative links, gets current directory and appends new filename
}

function nodots($path) { //Resolve dot dot slashes, no regex!
 $arr1 = explode('/',$path);
 $arr2 = array();
 foreach($arr1 as $seg) {
  switch($seg) {
   case '.':
    break;
   case '..':
    array_pop($arr2);
    break;
   case '...':
    array_pop($arr2); array_pop($arr2);
    break;
   case '....':
    array_pop($arr2); array_pop($arr2); array_pop($arr2);
    break;
   case '.....':
    array_pop($arr2); array_pop($arr2); array_pop($arr2); array_pop($arr2);
    break;
   default:
    $arr2[] = $seg;
  }
 }
 return implode('/',$arr2);
}

Usage Example:

echo nodots(absurl('../index.html'));

nodots() must be called after the URL is converted to absolute.

The dots function is kind of redundant, but is readable, fast, doesn't use regex's, and will resolve 99% of typical urls (if you want to be 100% sure, just extend the switch block to support 6+ dots, although I've never seen that many dots in a URL).

Hope this helps,

Aaron Gillion
  • 2,227
  • 3
  • 19
  • 31