Can't display german umlauts even if UTF-8 is

2019-07-20 08:38发布

问题:

Let me explain situation closer.

  1. Im working on some old company server with PHP 4. There is Windows 2000NT installed and a databaze that I can open with Microsoft Access 2000.
  2. There is no any option to set character encoding (unlike in phpMyAdmin), but I can read that characters in MS Access properly.
  3. Im extracting data from that database in my .php files and displaying it on my website, but characters like: ü, ä, ß are displayed wrong, like this: �
  4. All php/html files are saved by some old version of Notepad++ as UTF-8 encoding and containing: <!DOCTYPE html> <html lang="de"> <head> <meta charset="utf-8">

I read some theory about character encoding, history of character encoding and almighty UTF-8 that will solve all your problems, which is simply not true. What could be wrong?

回答1:

First, identify what byte values your broken characters have exactly. Without knowing you cannot identify the encoding like to be used.

echo urlencode($string_with_umlauts);

This will print all non-ascii characters as percent-encoded hex values. Note that this function is meant for some other purpose, but it'll help in this case also.

Then lookup the bytes in encoding tables like Wikipedia and be sure what you have there.

The last step: Add a transformation layer to your database access logic that converts from the encoding you saw to UTF-8 with iconv functions.



回答2:

Well, I found solution:

function decode($string){

$string = urlencode($string);
$string = str_replace('%DF','ß',$string);
$string = str_replace('%E4','ä',$string);
$string = str_replace('%F6','ö',$string);
$string = str_replace('%2B','+',$string);
$string = str_replace('%FC','ü',$string);
$string = str_replace('%26','&',$string);
$string = str_replace('%2F','/',$string);
$string = str_replace('%0A','',$string);
$string = str_replace('%0D','',$string);
$string = str_replace('%40','@',$string);
$string = str_replace('%2C',',',$string);
$string = str_replace('%E1','á',$string);
$string = str_replace('%D3','ó',$string);
$string = str_replace('+',' ',$string);

return $string;

}

But isn't there any better solution?