3v4l.org

run code in 300+ PHP versions simultaneously
<?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 ";}

preferences:
37.25 ms | 402 KiB | 5 Q