note 54211 added to function.parse-url

From: Date: Mon, 27 Jun 2005 13:43:57 +0000
Subject: note 54211 added to function.parse-url
Groups: php.notes 
Request: Send a blank email to php-notes+get-91019@lists.php.net to get a copy of this message
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 ) */ ?> ---- Manual Page -- http://www.php.net/manual/en/function.parse-url.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54211 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54211&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54211&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54211&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54211&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54211&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54211&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54211&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#91019) next »