note 37272 deleted from function.rawurldecode by nlopess

From: Date: Tue, 13 Jan 2004 14:47:27 +0000
Subject: note 37272 deleted from function.rawurldecode by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-63426@lists.php.net to get a copy of this message
Note Submitter: mina86@tlen.pl ---- his seems to be faster way: <?php function utf8rawurldecode($source) { $decoded = ''; while (($pos = strpos($source, '%'))!==false) { $decoded .= substr($source, 0, $pos); if (substr($source, $pos + 1, 1)=='u') { $decoded = '&#x' . substr($source, $pos + 2, 4); $pos += 5; } else { $decoded = chr(hexdec(substr($source, $pos + 1, 2))); $pos += 2; } $source = substr($source, $pos); } return $decoded . $source; } ?> Or even faster way using regexps (will change all encoded chars to &#xhex; form so it could lead to some problems): <?php preg_rawurldecode($source) { return preg_replace( '/%(([a-fA-F0-9]{2})|u([a-fA-F0-9]{4}))/' '&#x$2$3', $source); } ?> Note also that te 2nd example won't change (invalid) URL like '%0z' in oposite to the 1st one which behaves in the fallowing way (it is also valid for ronen's function): 1. if there is 'u' after '%' function adds '&#x$$$$;' to decoded string, where '$$$$' are 4 chars after '%u'; 2. if first character after '%' is valid hex digit (and 2nd isn't) it will be converted to dec value and characeter of such ASCII code will be added to decoded string; 3. if the first character after '%' is invalid hex digit then NULL char will be added to decoded string.

« previous php.notes (#63426) next »