note 101401 added to function.urldecode

From: Date: Wed, 15 Dec 2010 02:27:04 +0000
Subject: note 101401 added to function.urldecode
Groups: php.notes 
Request: Send a blank email to php-notes+get-174928@lists.php.net to get a copy of this message
When the client send Get data, utf-8 character encoding have a tiny problem with the urlencode. Consider the "º" character. Some clients can send (as example) foo.php?myvar=%BA and another clients send foo.php?myvar=%C2%BA (The "right" url encoding) in this scenary, you assign the value into variable $x $x = $_GET['myvar']; $x store: in the first case "�" (bad) and in the second case "º" (good) To fix that, you can use this function: function to_utf8( $string ) { // From http://w3.org/International/questions/qa-forms-utf-8.html if ( preg_match('%^(?: [\x09\x0A\x0D\x20-\x7E] # ASCII | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 )*$%xs', $string) ) { return $string; } else { return iconv( 'CP1252', 'UTF-8', $string); } } and assign in this way: $x = to_utf8( $_GET['myvar'] ); $x store: in the first case "º" (good) and in the second case "º" (good) Solve a lot of i18n problems. Please fix the auto-urldecode of $_GET var in the next PHP version. Bye. Alejandro Salamanca ---- Server IP: 69.147.83.197 Probable Submitter: 200.105.200.90 ---- Manual Page -- http://www.php.net/manual/en/function.urldecode.php Edit -- https://master.php.net/note/edit/101401 Del: integrated -- https://master.php.net/note/delete/101401/integrated Del: useless -- https://master.php.net/note/delete/101401/useless Del: bad code -- https://master.php.net/note/delete/101401/bad+code Del: spam -- https://master.php.net/note/delete/101401/spam Del: non-english -- https://master.php.net/note/delete/101401/non-english Del: in docs -- https://master.php.net/note/delete/101401/in+docs Del: other reasons-- https://master.php.net/note/delete/101401 Reject -- https://master.php.net/note/reject/101401 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#174928) next »