Peter
Peter

Reputation: 1894

Can't display german umlaut

My main problem right now is that I can't display "ä, ü, ö" on my webpage, the get displayed as �, when I display my script with "echo" they seem normal, when I run it included from my html page I get �.

After I tried several things I've read on this topic I couldn't find a solution to my problem.

I'm reading an .xls file with a .xls to php reader. I get the data from the fields like this:

function getMenueMonday($connection) {
    $menu = "";
    $startrow = 8;
    $endrow = 16;
    $col1 = 3;
    for ($i = $startrow; $i < $endrow; $i++) {
        $menu .= $connection->sheets[0]["cells"][$i][$col1] . "<br>";
    }
    return $menu; }

Which runs perfectly fine. Then I'm saving all these return values in an array. I loop trough them to save them into a mysql database.

for ($i = 0; $i < 5; $i++) {
    insertMenue(changeAttr($menues[$i]), $days[$i]);
}

The method I'm using to change the letters is as following:

function changeAttr($content) {
    $search = array("ä", "ö", "ü", "ß", "Ä", "Ö", "Ü");
    $replace = array("&auml;", "&ouml;", "&uuml;", "ss", "&Auml;", "&Ouml;", "&Uuml;");
    $contentrep = str_replace($search, $replace, $content);
    return $contentrep;
}

In the table of my database they still get saved as "ä, ö, ü". The table itself is in utf8_bin. I tried this in the header of the html file:

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<html lang="de">

as well as this in the file I use to read the db.

header('Content-Type: text/html; charset=utf-8');

With following code I get this output:

foreach($menues as $r){
    echo urlencode($r);
}

"Saisonfr%FCchte" where it should be "Saisonfrüchte".

Is there a point where I miss the charset or do something particularly wrong?

Upvotes: 2

Views: 2553

Answers (2)

Peter
Peter

Reputation: 1894

Well now actually everything after an "ä, ö, ü" doesn't get written into the database... But if there was one in the db, it would be displayed correctly.

Upvotes: 0

Gerard van Helden
Gerard van Helden

Reputation: 1602

Your connection must be aware of the encoding you're using as well, otherwise some implicit character set conversion will take place. To achieve that, you can do:

SET NAMES utf8

Please refer to this blog post for a step-by-step explanation of common encoding errors when using MySQL and PHP.

If everyone is "speaking utf-8", there is no need for escaping the characters in your HTML using named entity references. It may even be considered bad practice.

Upvotes: 6

Related Questions