note 37272 added to function.rawurldecode

From: Date: Sat, 08 Nov 2003 01:37:17 +0000
Subject: note 37272 added to function.rawurldecode
Groups: php.notes 
Request: Send a blank email to php-notes+get-60132@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/function.rawurldecode.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37272 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37272&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37272&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#60132) next »