3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function make_absolute_path($a,$b){if((!$c=parse_url($a))||(!$d=parse_url($b))){return FALSE;}if(empty($c['host'])&&!empty($c['path'])&&substr($c['path'],0,2)==='//'){$e=explode('/',ltrim($c['path'],'/'));$c['host']=array_shift($e);$c['path']='/'.implode('/',$e);}if(empty($d['host'])&&!empty($d['path'])&&substr($d['path'],0,2)==='//'){$e=explode('/',ltrim($d['path'],'/'));$d['host']=array_shift($e);$d['path']='/'.implode('/',$e);}if(!empty($d['host'])){return $b;}if(empty($c['host'])){return FALSE;}if(empty($c['path'])){$c['path']='/';}if(empty($c['scheme'])){$c['scheme']='http';}$f=$c['scheme'].'://';if(!empty($c['user'])){$f.=$c['user'];if(!empty($c['pass'])){$f.=":{$c['pass']}";}$f.='@';}$f.=!empty($c['port'])?"{$c['host']}:{$c['port']}":$c['host'];if($b[0]==='/'){$f.=$b;}else if($b[0]==='?'){$f.=$c['path'].$b;}else{$g=rtrim(substr($c['path'],-1)==='/'?trim($c['path']):str_replace('\\','/',dirname(trim($c['path']))),'/');foreach(explode('/',$b)as $h){switch($h){case '':case '.':break;case '..':$g=rtrim(str_replace('\\','/',dirname($g)),'/');break;default:$g.="/$h";break;}}$f.=$g;}return $f;}$i=array(1=>array('arg1'=>'http://www.xxx.com/dir/index.php','arg2'=>'/photos/photo.jpg','expect'=>'http://www.xxx.com/photos/photo.jpg'),array('arg1'=>'http://www.xxx.com/dir/index.php','arg2'=>'/photos/photo.jpg','expect'=>'http://www.xxx.com/photos/photo.jpg'),array('arg1'=>'http://www.xxx.com/dir/index.php','arg2'=>'photos/photo.jpg','expect'=>'http://www.xxx.com/dir/photos/photo.jpg'),array('arg1'=>'http://www.xxx.com/dir/index.php','arg2'=>'./photos/photo.jpg','expect'=>'http://www.xxx.com/dir/photos/photo.jpg'),array('arg1'=>'http://www.xxx.com/dir/index.php','arg2'=>'../photos/photo.jpg','expect'=>'http://www.xxx.com/photos/photo.jpg'),array('arg1'=>'http://www.xxx.com/dir/index.php','arg2'=>'//www.yyy.com/photos/photo.jpg','expect'=>'//www.yyy.com/photos/photo.jpg'),array('arg1'=>'http://www.xxx.com/dir/index.php','arg2'=>'?query=something','expect'=>'http://www.xxx.com/dir/index.php?query=something'),array('arg1'=>'http://www.xxx.com/dir/index.php','arg2'=>'?query=something#fragment','expect'=>'http://www.xxx.com/dir/index.php?query=something#fragment'),array('arg1'=>'http://www.xxx.com/dir/index.php','arg2'=>'/photos/photo.jpg#fragment','expect'=>'http://www.xxx.com/photos/photo.jpg#fragment'),array('arg1'=>'http://www.xxx.com/dir/index.php','arg2'=>'../../photos/photo.jpg','expect'=>'http://www.xxx.com/photos/photo.jpg'),array('arg1'=>'http://www.xxx.com/dir/sub1/sub2/index.php','arg2'=>'../../photo/photo.jpg','expect'=>'http://www.xxx.com/dir/photo/photo.jpg'),array('arg1'=>'//www.xxx.com/dir/index.php','arg2'=>'test?query=something','expect'=>'http://www.xxx.com/dir/test?query=something'));foreach($i as $j=>$k){$f=make_absolute_path($k['arg1'],$k['arg2']);echo" Test #$j (".($f===$k['expect']?'PASS':'FAIL').") Arg 1: {$k['arg1']} Arg 2: {$k['arg2']} Expect: {$k['expect']} Result: $f ";}
Output for 4.3.0 - 7.1.0
Test #1 (PASS) Arg 1: http://www.xxx.com/dir/index.php Arg 2: /photos/photo.jpg Expect: http://www.xxx.com/photos/photo.jpg Result: http://www.xxx.com/photos/photo.jpg Test #2 (PASS) Arg 1: http://www.xxx.com/dir/index.php Arg 2: /photos/photo.jpg Expect: http://www.xxx.com/photos/photo.jpg Result: http://www.xxx.com/photos/photo.jpg Test #3 (PASS) Arg 1: http://www.xxx.com/dir/index.php Arg 2: photos/photo.jpg Expect: http://www.xxx.com/dir/photos/photo.jpg Result: http://www.xxx.com/dir/photos/photo.jpg Test #4 (PASS) Arg 1: http://www.xxx.com/dir/index.php Arg 2: ./photos/photo.jpg Expect: http://www.xxx.com/dir/photos/photo.jpg Result: http://www.xxx.com/dir/photos/photo.jpg Test #5 (PASS) Arg 1: http://www.xxx.com/dir/index.php Arg 2: ../photos/photo.jpg Expect: http://www.xxx.com/photos/photo.jpg Result: http://www.xxx.com/photos/photo.jpg Test #6 (PASS) Arg 1: http://www.xxx.com/dir/index.php Arg 2: //www.yyy.com/photos/photo.jpg Expect: //www.yyy.com/photos/photo.jpg Result: //www.yyy.com/photos/photo.jpg Test #7 (PASS) Arg 1: http://www.xxx.com/dir/index.php Arg 2: ?query=something Expect: http://www.xxx.com/dir/index.php?query=something Result: http://www.xxx.com/dir/index.php?query=something Test #8 (PASS) Arg 1: http://www.xxx.com/dir/index.php Arg 2: ?query=something#fragment Expect: http://www.xxx.com/dir/index.php?query=something#fragment Result: http://www.xxx.com/dir/index.php?query=something#fragment Test #9 (PASS) Arg 1: http://www.xxx.com/dir/index.php Arg 2: /photos/photo.jpg#fragment Expect: http://www.xxx.com/photos/photo.jpg#fragment Result: http://www.xxx.com/photos/photo.jpg#fragment Test #10 (PASS) Arg 1: http://www.xxx.com/dir/index.php Arg 2: ../../photos/photo.jpg Expect: http://www.xxx.com/photos/photo.jpg Result: http://www.xxx.com/photos/photo.jpg Test #11 (PASS) Arg 1: http://www.xxx.com/dir/sub1/sub2/index.php Arg 2: ../../photo/photo.jpg Expect: http://www.xxx.com/dir/photo/photo.jpg Result: http://www.xxx.com/dir/photo/photo.jpg Test #12 (PASS) Arg 1: //www.xxx.com/dir/index.php Arg 2: test?query=something Expect: http://www.xxx.com/dir/test?query=something Result: http://www.xxx.com/dir/test?query=something