简体   繁体   中英

PHP urlencode charset encoding issue

I got this php script:

$str = "ú";
echo $str . ' -> ' . urlencode($str) . "\n" ;

Expected Result:

ú -> %FA

Reference: http://www.w3schools.com/tags/ref_urlencode.asp

Actual Result

ú -> %C3%BA

Try this:

urlencode(utf8_decode($str));

That should give you the expected result.

You encode the ú as UTF-8 (check the encoding of your example code), so urlencode does correctly encode it as %C3%BA .

You were more or less referring to this:

$str = "\xFA"; # ú in LATIN-1
echo $str . ' -> ' . urlencode($str) . "\n" ;

Which gives you your expected result, regardless how you encode the php-code/-file:

ú -> %FA

Demo , that site is using UTF-8 to store the source-code. If you want the output displayed as LATIN-1, this additional example signals the browser the LATIN-1 charset:

header('Content-Type: text/html; charset=latin-1');
$str = "\xFA"; # ú in LATIN-1
echo $str . ' -> ' . urlencode($str) . "\n" ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM