ho notato la risposta upvoted precedente utilizza RegEx, che può essere pericoloso quando si tratta di URL.
Questa funzione risolverà URL relativi ad un determinato pagina corrente url nella $pgurl
senza regex. Si risolve con successo:
/home.php?example
tipi,
stessi-dir nextpage.php
tipi,
../...../.../parentdir
tipi,
pieni http://example.net
gli URL,
e stenografia //example.net
gli URL
//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);
}
Uso Esempio:
echo nodots(absurl('../index.html'));
nodots()
deve essere chiamato dopo l'URL viene convertito in assoluto.
La funzione punti è una sorta di ridondante, ma è leggibile, veloce, non usa regex e risolverà il 99% degli URL tipici (se si vuole essere sicuri al 100%, basta estendere il blocco switch per supportare 6 + punti, anche se non ho mai visto tanti punti in un URL).
Spero che questo aiuti,
Ecco [un confronto] (http://scraperblog.blogspot.com/2012/12/convert- relative-urls-to-absolute-in.html) di alcune delle soluzioni menzionate più una delle mie. – pguardiario
Questo codice ha fatto il trucco per me: http://sourceforge.net/projects/absoluteurl/ –
dublicates: http://stackoverflow.com/questions/4444475/transfrom-relative-path-into-absolute-url-using -php http://stackoverflow.com/questions/11653677/php-relative-urls-to-absolute-urls-conversion-with-eventually-base-href-html-tag http://stackoverflow.com/questions/19618754/convert-relative-url-url-assoluto- http://stackoverflow.com/questions/26423904/converting-relative-url-to-solutions – qdinar