note 81822 added to function.parse-url

From: Date: Fri, 14 Mar 2008 15:05:24 +0000
Subject: note 81822 added to function.parse-url
Groups: php.notes 
Request: Send a blank email to php-notes+get-137063@lists.php.net to get a copy of this message
Please note that parse_url seems not to produce always the same results when passing non-standard urls. Eg. I was using this code since 2005 (both under PHP 4.3.10 and PHP 5.2.3) : <?php $p = parse_url ( 'http://domain.tld/tcp://domain2.tld/dir/file' ) ; $d2 = parse_url ( $p['path'] ) ; echo $d2 ; // returns '/dir/file' ?> Of course my example is very specific, as URL is not really correct. But using parse_url was a great trick to split URL easily (without using regular expressions). Unfortunately under PHP 5.2.0-8 (+etch10), parse_url will fail as it does not accept the slash (/) at the beginning of URL. Here is a possible patch : <?php $p = parse_url ( 'http://domain.tld/tcp://domain2.tld/dir/file' ) ; $d2 = parse_url ( substr ( $p['path'] , 1 ) ) ; echo $d2 ; // returns '/dir/file' ?> However this last code is not optimized at all, and should be replaced by a regular expression to split URL (so that parse_url would be no longer used). So you should use parse_url very carefully, and verify that you pass only standard URLs... ---- Server IP: 217.174.223.124 Probable Submitter: 193.55.175.20 ---- Manual Page -- http://www.php.net/manual/en/function.parse-url.php Edit -- https://master.php.net/note/edit/81822 Del: integrated -- https://master.php.net/note/delete/81822/integrated Del: useless -- https://master.php.net/note/delete/81822/useless Del: bad code -- https://master.php.net/note/delete/81822/bad+code Del: spam -- https://master.php.net/note/delete/81822/spam Del: non-english -- https://master.php.net/note/delete/81822/non-english Del: in docs -- https://master.php.net/note/delete/81822/in+docs Del: other reasons-- https://master.php.net/note/delete/81822 Reject -- https://master.php.net/note/reject/81822 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#137063) next »