doufen1933
2017-02-22 05:35
浏览 1.1k
已采纳

处理json_encode中的特殊字符

In my database, there are many special characters like (İstanbul, Yeşilköy, Atatürk and more).

when I directly fetch these data from the database all working fine. Means data remain original but problem occur when I convert these data to JSON using json_encode().

After encoding all these data converted to some strange characters like ?stanbul, Bak?rköy...

But after decodeing (using json_decode) I need the original string which was before encoding(json_encode).

I tried many solutions provided on the web to solve the problem but none of them is fully working in my case.

some solutions I tried-

If not possible to get the original string atleast there should not be any ? like thing(remove these things).

Suggest me something to solve the issue.

图片转代码服务由CSDN问答提供 功能建议

在我的数据库中,有许多特殊字符,如(İstanbul,Yeşilköy,Atatürk等)。

当我直接从数据库中获取这些数据时,一切正常。 意味着数据保持原始状态,但是当我使用json_encode()将这些数据转换为JSON时会出现问题。

将所有这些数据编码后转换为一些奇怪的字符,如 ?stanbul,Bak?rköy...

但解码后(使用json_decode) )我需要编码之前的原始字符串(json_encode)。

我尝试了网上提供的许多解决方案来解决问题,但在我的情况下,它们都没有完全正常工作。

我试过的一些解决方案 -

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douchen2011 2017-12-01 07:38
    已采纳

    This library saved my life(https://github.com/neitanod/forceutf8).

    Previously I was using the wrong method to get the issue resolved. toUTF8() function was life saver.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dqch34769 2017-02-22 05:48

    Since PHP 5.4 you can use this :

    json_encode( $text, JSON_UNESCAPED_UNICODE );
    
    评论
    解决 无用
    打赏 举报
  • dth981485742 2017-06-07 10:57

    Use mysqli_set_charset(connection,charset); before mysqli_query();

    Example:

    mysqli_set_charset( $CONNECTION, "utf8" );
    
    $result_query = mysqli_query( $CONNECTION, $query );
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题