problems with german umlauts in php json_encode(php json_encode 中德语变音的问题)
问题描述
我在处理包含德国变音符号的数据库中的数据时遇到问题.基本上,每当我收到包含变音的数据时,它都是一个带有询问标记的黑色方块.我解决了这个问题
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')
在查询之前.
问题是,只要我在查询结果上使用 json_encode(...),包含变音的值就会获得 null.我可以通过直接在浏览器中调用 php 文件来看到这一点.除了在编码为 JSON 并在 JS 中解码之前替换此字符之外,还有其他解决方案吗?
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?
推荐答案
您可能只想在浏览器中以某种方式显示文本,因此一种选择是使用 htmlentities().
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().
以下测试对我有用:
<?php
$test = array( 'bla' => 'äöü' );
$test['bla'] = htmlentities( $test['bla'] );
echo json_encode( $test );
?>
这篇关于php json_encode 中德语变音的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:php json_encode 中德语变音的问题
基础教程推荐
- 如何替换eregi() 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
