简体   繁体   中英

php $_GET doesn't work correctly with ISO-8859-1 characters

I got a database that contains accented letters (like "é" etc) in several fields. If I echo those on my pages they are displayed correctly, because I use this html-tag when I render my html:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

But when I want to display a value from a parameter from my URL with $_GET it doesn't work.

I use this code:

$fname = $_GET['fname'];

Actual output:

groté

Desired output:

Groté

The encoding looks OK to me, I checked it using this code:

var_dump($fname);
var_dump(iconv_get_encoding('all'));

Output:

string(6) "groté" 
array(3) { ["input_encoding"]=> string(10) "ISO-8859-1" ["output_encoding"]=> string(10) "ISO-8859-1" ["internal_encoding"]=> string(10) "ISO-8859-1" }

I don't use code where I set the encoding in php. I only use the meta tag, like noted above.

What's wrong with my code?

OK, after further investigation I found a solution!

$fnameraw = $_GET['fname'];
$fname = utf8_decode($fnameraw);

Now I got the correct output: groté ! While using charset=ISO-8859-1 in my meta tag. Both the database-output as the output from the URL display correctly.

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