How to: MySQL PHP umlauts and glyphs not displayed correct UTF-8 to ISO

Everyone knows the problem, that for some reason strings are written in the wrong encoding into the database. If this happened, it can be seen that, that characters like these appear:

'¦, '¨, '?, '´, '¸, 'À, 'Á, 'Â, 'Ã, 'Ä, 'Å, 'Æ, 'Ç, 'È, 'É, 'Ê, 'Ë,
'Ì, 'Í, 'Î, 'Ï, 'Ñ, 'Ò, 'Ó, 'Ô, 'Õ, 'Ö, 'Ø, 'Ù, 'Ú, 'Û, 'Ü, 'Ý, 'Þ,
'ß, 'à, 'á, 'â, 'ã, 'ä, 'å, 'æ, 'ç, 'è, 'é, 'ê, 'ë, 'ì, 'í, 'î,
'ï, 'ð, 'ñ, 'ò, 'ó, 'ô, 'õ, 'ö, 'ø, 'ù, 'ú, 'û, 'ý, 'þ, 'ÿ

The problem is, that these characters are not encoded in utf8, but in utf8 been depicted are, What is a Variety of reasons can have.

Convert ISO encoded strings to UTF-8

To avoid this,, try

$string = utf8_encode($string);


Review the coding

The encoding of strings can be checked with the function mb_detect_encoding.

echo mb_detect_encoding($string);

For a quick and dirty fix you can take the following solution:

if(mb_detect_encoding($string) != 'UTF-8') {          $string = utf8_encode($string); }

Change the database connection coding

Another source of error, is the transfer of data to the database, This should be used always once after opening the database connection to UTF-8:

mysql_query("SET NAMES 'utf8'");

Download UTF-8 encoded PHP files into an ISO-encoded project

If accidentally UTF-8 encoded PHP files are loaded, it may occur, that in spite of all effort on UTF-8 encoding is converted.

The following will help:

require_once "utf-8.php"
header('Content-Type: text/html; charset=ISO-8859-1');

Helper function to encode arrays to UTF-8

UTF-8 to encode a simple recursive function to a multidimensional array is (performance would be to work with references):

function utf8encodeArray($array)
        foreach($array as $key =>  $value)
                $array[$key] = utf8encodeArray($value);
            elseif(!mb_detect_encoding($value, 'UTF-8', true))
                $array[$key] = utf8_encode($value);

The header

It should also be checked, If the header of the HTML document is set to UTF8:

       <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

or with PHP

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

File encoding

The encoding of the PHP file must be UTF-8 also, otherwise umlauts and glyphs will be destroyed (can e.g.. with Notepad checked and changed: Main Menu>Coding>UTF-8 ). In any good IDE the encoding can be changed or e set (on project or file basis).

Other sources of error

You can lose the encoding of strings or document in many ways. Particularly insidious are PHP functions for string manipulation, automatically convert the string to UTF8 and return, What is a problem, If the Web page is encoded in ISO-8859-1. Unfortunately, I have found no longer functions, I would love feedback on the topic.

If it is too late and the data have been stored in the database, one can replace the wrong umlauts as follows:

private function getUmlauteArray() { return array( 'ü'=>'ü', 'ä'=>'ä', 'ö'=>'ö', 'Ö'=>'Ö', 'ß'=>'ß', 'à '=>'à', 'á'=>'á', 'â'=>'â', 'ã'=>'ã', 'ù'=>'ù', 'ú'=>'ú', 'û'=>'û', 'Ù'=>'Ù', 'Ú'=>'Ú', 'Û'=>'Û', 'Ãœ'=>'Ü', 'ò'=>'ò', 'ó'=>'ó', 'ô'=>'ô', 'è'=>'è', 'é'=>'é', 'ê'=>'ê', 'ë'=>'ë', 'À'=>'À', 'Á'=>'Á', 'Â'=>'Â', 'Ã'=>'Ã', 'Ä'=>'Ä', 'Ã…'=>'Å', 'Ç'=>'Ç', 'È'=>'È', 'É'=>'É', 'Ê'=>'Ê', 'Ë'=>'Ë', 'ÃŒ'=>'Ì', 'Í'=>'Í', 'ÃŽ'=>'Î', 'Ï'=>'Ï', 'Ñ'=>'Ñ', 'Ã’'=>'Ò', 'Ó'=>'Ó', 'Ô'=>'Ô', 'Õ'=>'Õ', 'Ø'=>'Ø', 'Ã¥'=>'å', 'æ'=>'æ', 'ç'=>'ç', 'ì'=>'ì', 'í'=>'í', 'î'=>'î', 'ï'=>'ï', 'ð'=>'ð', 'ñ'=>'ñ', 'õ'=>'õ', 'ø'=>'ø', 'ý'=>'ý', 'ÿ'=>'ÿ', '€'=>'€' );

public function fixeUmlauteDb() {                  $umlaute = $this->getUmlauteArray();                  foreach ($umlaute as $key => $value){                                         $sql = "UPDATE table SET tracks = REPLACE(row, '{$key}', '{$value}') WHERE row LIKE '%{$key}%'";                   } }

Attention: The script works only, Who is the encoding of the PHP file is UTF-8 (can e.g.. be changed checked with Notepad /: Main Menu>Coding>UTF-8 ).

The script only works with a UTF8 encoded project (see article: PHP puzzle).

Hat Ihnen der Artikel geholfen bei Ihrem Umlaute-Problem?

Ergebnis der Umfrage anzeigen

Loading ... Loading ...

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  1. ISO encodings never see “random” like valid UTF-8, therefore can be checked clearly, whether a straight Windows alias is available ISO-8859-1 encoding or not.

    if ( (strlen($s) > 0) && ‘ISO-8859-1′ == mb_detect_encoding($search, ‘UTF-8, ISO-8859-1′, true))
    $s = utf8_encode( $s );

    • Thanks for pointing, I did not know the function, I've grown in the same article!

  2. >The problem is, that these characters are not encoded in utf8, but have been stored in utf8, because the DB column has been defined as (Kolation: utf8_general e.g.:).

    In my experience, this looks so if strings in a multibyte character set (z.B. UTF-8) exist and are stored in a DB column, has the a single-byte character set (z.B. latin1). e.g. or three bytes are taken together actually a special character – the single byte character individually interprets each byte and saves it as.

    But there are many (common) Combinations can be something in the DB.

  3. Hello, input is: “The problem is, that these characters are not encoded in utf8, but have been stored in utf8, because the DB column has been defined as (Kolation: utf8_general e.g.:).”
    1. It says “Collation”, so with two “L”
    2. only the collation is the collation, Here is not the problem but, This is the character set (encoding).
    Please correct.
    Greeting franc

    • Thank you very much for your help franc, is changed. You're completely right, that the collation is only the sort order.
      This affected, whether the Ä as ae or a when sorting is replaced z.B.

  4. Hello Sebastian,

    I have a problem. And while I write in my DB data. The individual pages are encoded in UTF-8 without BOM and the database also.
    If I submit the information with “mb_detect_encoding” check, then will I also “UTF-8″ returned.

    And yet I have to say the strange characters in my database. When I again read out the data, then everything is OK – other words on the page, there are apparently no problems.

    I have the command “mysql_query(“SET NAMES'utf8′”);” tried, before I write the data in the DB. Thus, the data in the DB is correct, so without special characters, However, I have the hieroglyphics then on the page again.

    The header is set to UTF-8.

    Can say what?
    Thank you in advance!

    • Hi,
      It may be only on the file encoding, diwe on ISO is. Attempt to change the file in Notepad ….

  5. In the URL, special characters is available but I find nirgendsetwas to special characters. So I was here not looking for

  6. Thanks that someone makes the effort Yes super.
    Has helped me to clean up my DB entries.

    Many thanks to Sebastian

  7. The PHP function strtolower works with the current locale – that are sometimes problems with the encoding. Instead, use mb_strtolower.

  8. Unfortunately, I still seem to be another problem with the German umlauts: they are with me all through a ? but they missing or replaced. I'm trying to show my approach:

    When importing the data into the MySQL DB I have used the following notation:

    CREATE TABLE IF emergency EXISTS 't_MeineTabelle'(
    'ID' INT,
    'Abk' varchar(10),
    'Comment' TEXT

    INSERT INTO t_MeineTabelle(ID,ABK,Comment)VALUES
    (1,' s1′,' beautiful),
    (2,' s2′,'more beautiful'),
    (3,' s3′,'äöüß');

    When I get these entries with PHP, do I open the database with these commands:
    $_SESSION[' link '] = @ mysql_connect( $$db_server, $db_user, $db_passwort )
    or that( ' Could establish a connection to the database.’ );

    if ( !mysql_select_db()( $db_name, $_SESSION[' link '] ) ) {
    the( ' Could not select database schema!’ );
    mysql_query(“SET NAMES'utf8′”);

    Accessing the data so:
    $result = mysql_query( $SQL, $_SESSION[' link '] );
    for ($i = 0; $i < mysql_num_rows($result); $i ) {
    if (!mysql_data_seek($result, $i)) {
    $strFehler = "indicated seek to row $i: " . mysql_error() . "n";
    if (!($row = mysql_fetch_assoc($result))) {
    $Output = $row['Comment'];

    Of course, I'm closing the database connection to the end of the php file:

    the umlauts are as ? shown, the ß is completely absent.

    Does anyone have an idea, How can I fix my problem?
    Thank you in advance.

    • Hi … I have the following problem I want all characters of a string (UTF-8) the umlaut exploden via str_split contains() Unfortunately this does not work properly. Have tried already different transformations, unfortunately without success. Who can help ?

      • This happens, If a string was encoded twice. Or double decodes. One of the two anyway. So the string was z.B. already UTF-8 encoded and it was yet again the utf8_encode – Function called.

        By the way, should be not fooled, Since it's on the browser, a character is represented as. It can be so, It doesn't seem a sign, how it is actually stored. So not only look, What's in the database, but also, whether the performing Web page uses the right character set:

        So to understand even with the Web page play around and enter at the very beginning:
        1. header(' Content-type: text/html; Charset = UTF-8;’);
        2. header(' Content-type: text/html; charset = ISO-8859-1′);
        3. omit times.
        Print but always the same string.

        Strings coming from emails, then a string of also quoted printable can be encoded. Send time to test your string through the following functions and give the return value with echo off:

        function displayEncoded($data)
        $encoding = mb_detect_encoding($data, “car”);
        return imap_qprint(mb_convert_encoding($data, $encoding));

        function displayDecoded($data)
        return imap_8bit($data);

  9. Hello,
    This article has been the most extensive in the help on this issue. But unfortunately he has not helped me with my problem, What me almost to the freak out slowly brings.
    There are still other sources of error / Problems as described above?
    I have a Web space from a provider (so no dedicated server, only Web space).
    When programming, I'm newbie still quasi, But what is at stake, I get it. Only the expertise is missing stop (do not do this).

  10. Super – Hat mir sehr geholfen – was already at renownranks with the schei… are to…

    Thank you!!

  11. Hi... I have the following problem:

    I want all characters of a string (UTF-8) the umlaut exploden via str_split contains() to write each character into an array – Unfortunately this does not work properly.

    Have tried already different transformations, unfortunately without success. The special characters z.B. Umlauts are not recognized or. I don't know in which encoding. Also mb_detect_encoding() has not brought me.

    Who can help ?

    • I found now a solution :

      $str_enc = mb_convert_encoding('Öaßbsscü','Unicode');
      $str_dec = mb_convert_encoding($str_enc,' UTF-8′,'ASCII');

      Then it works !