note 54211 deleted from function.parse-url by nlopess

From: Date: Tue, 11 Oct 2005 20:09:18 +0000
Subject: note 54211 deleted from function.parse-url by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-96855@lists.php.net to get a copy of this message
Note Submitter: Hans at pennywaffer dot net ---- The URL components provided by parse_url() can get messed up under the rare condition in which a different URL is part of the path structure and no scheme or host is given at the beginning of the main URL. The following URL is taken from a href on www.imdb.com: <?php //a linebreak was added to make the URL fit in this textarea $url = "/ri/TRAILERS_HPWW/TOP_BUCKET/19750/http://www.fandango.com/ MoviePage.aspx?mid=2348&source=otw_title"; $parts = parse_url($url); print_r($parts); /* Will output: Array ( [scheme] => ri/TRAILERS_HPWW/TOP_BUCKET/19750/http [host] => www.fandango.com [path] => /MoviePage.aspx [query] => mid=2348&source=otw_title ) */ ?> Apparently PHP defines the scheme component as the range from the start of the url to the last occurance of a possible scheme, whereas it should cut it off after the first occurance. One could fix this using the following code, however this is not very effecient and requires manual specification of every possible scheme. <?php if (!empty($parts['scheme']) && !eregi("^(https?|ftp|mailto|news|telnet)$", $parts['scheme'])) { //rebuild path structure $parts['path'] = $parts['scheme']."://".$parts['host'].$parts['path']; unset($parts['scheme']); unset($parts['host']); } print_r($parts); /* will output: Array (//again a linebreak was added to make it fit [path] => /ri/TRAILERS_HPWW/TOP_BUCKET/ 19750/http://www.fandango.com/MoviePage.aspx [query] => mid=2348&source=otw_title ) */ ?>

« previous php.notes (#96855) next »