问题与德语umlauts在php json_encode [英] problems with german umlauts in php json_encode

查看:191
本文介绍了问题与德语umlauts在php json_encode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从包含德语变音的数据库的数据中得到麻烦。基本上,每当我收到包含umlauts的数据,它是一个带有询问标记的黑色正方形。我通过把

  mysql_query('SET NAMES utf8')
pre>



问题是,一旦我使用 json_encode(...)对查询的结果,包含umlaut的值获取 null 。我可以看到这通过直接在浏览器中调用php文件。有没有其他解决方案比替换这些字符编码前JSON和解码它在JS?

解决方案

您可能只想在浏览器中显示文本,所以一个选项是将变音符更改为HTML实体 htmlentities()



以下测试适用于我:

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

echo json_encode($ test);
?>


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?

解决方案

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 );
?>

这篇关于问题与德语umlauts在php json_encode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆