Viewed   219 times

I'm getting troubles with data from a database containing german umlauts. Basically, whenever I receive a data containing umlauts, it is a black square with an interrogation mark. I solved this by putting

mysql_query ('SET NAMES utf8')

before the query.

The problem is, as soon as I use json_encode(...) on a result of a query, the value containing an umlaut gets null. I can see this by calling the php-file directly in the browser. Are there other solution than replacing this characters before encoding to JSON and decoding it in JS?

 Answers

3

You probably just want to show the texts somehow in the browser, so one option would be to change the umlauts to HTML entities by using htmlentities().

The following test worked for me:

<?php
    $test = array( 'bla' => 'äöü' );
    $test['bla'] = htmlentities( $test['bla'] );

    echo json_encode( $test );
?>
Thursday, August 18, 2022
1

If you have made sure that both the tables, and the output encoding are UTF-8, almost the only thing left is the connection encoding.

The reason for the change in behaviour when updating servers could be a change of the default connection encoding:

[mysql]
default-character-set=utf8

However, I can't see any changes in the default encoding between versions, so if those were brand-new installs, I can't see that happening.

Anyway, what happens if you run this from within your PHP query and output the results. Any differences to the command line output?

 SHOW VARIABLES LIKE 'character_set%';
 SHOW VARIABLES LIKE 'collation%'; 
Friday, November 18, 2022
 
5

You could try to make your webpage utf-8, put this in your head tag:

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

strftime is depending on the right setting of the locale, so check your setlocale() and make sure that locale exists on the machine that php has running.

Update

I ran this code on my server:

setlocale(LC_ALL, 'de_DE');
$date[0] = mktime( 1, 0, 0, 3, 2, 2012 );

$date_day_month = strftime('%d. %B', $date[0]);
$date_year = strftime('%Y', $date[0]);

echo $date_day_month . $date_year;

And that outputs:

02. März2012

Friday, September 9, 2022
 
dmitry
 
4

Ok everyone - problem finally solved, thought I'd post it here for anyone else with similair problems. As it turns out, the problem occured earlier in the process.

The content of the SOAP reply was generated building the structure with a DomDocument. Later that was saved with the saveHTML function. As it turns out, that function adds some HTML encodings which breaks the soap decoding on the client.

When instead using the saveXML function the reply gets through successfully (also when adding tags and other strange stuff) and is also decoded to the correct strings by the soap client.

I hope this is the end of it, but you never know :)

Thanks for all the help and +1 on those helpful to checking the right places.

/Victor

Thursday, December 1, 2022
 
pv.
 
pv.
2

I use this:

function replaceSpecial($str){
$chunked = str_split($str,1);
$str = ""; 
foreach($chunked as $chunk){
    $num = ord($chunk);
    // Remove non-ascii & non html characters
    if ($num >= 32 && $num <= 123){
            $str.=$chunk;
    }
}   
return $str;
} 
Friday, October 14, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :