json_encode 不会编码法语字符 [英] json_encode won't encode French characters

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

问题描述

我正在尝试使用法语字符.无论出于何种原因,PHP 都不会正确输出它们,除非我强制使用 ISO-8859-1 字符集(我真的不想这样做);它不会让我使用 UTF-8.

因此,当我强制 ISO-8859-1 时对我的数组执行 print_r() 会产生以下结果:

数组([0] =>标准类对象([语言] =>fr[语言选择] =>一世[s1next] =>前链[hascodespan] =>Avez-vous déjà un code d'enregistrement?[s2prev] =>先例[s2next] =>Oui, j'ai déjà un 代码.[编码说明] =>Un E-Code vous donne un accès exclusif à des événements vraiment cool.Si vous êtes dans le besoin d'un code, vous pouvez en acheter un dans notre Boutique en ligne envisitant celien <a href="#">Boutique en ligne</a>.[购买] =>Cliquez ici 浇注 acheter en ligne 钢坯[ecodespan] =>S'il vous plaît entrer votre 电子代码[ecodelocdescription] =>Votre code peut être trouvé ci-dessous le code à barres sur votre billet[s3prev] =>先例[s3next] =>前链[验证文本] =>验证电子代码...... Un 即时.))

使用UTF-8时,输出如下:

数组([0] =>标准类对象([语言] =>fr[语言选择] =>[s1next] =>前链[hascodespan] =>Avez-vous d j  un code d'enregistrement?[s2prev] =>客户[s2next] =>Oui, j'ai d j  un 代码.[编码说明] =>Un E-Code vous donne un acc s exclusif   des  v nements vraiment cool.Si vous tes dans le besoin d'un code, vous pouvez en acheter un dans notre Boutique en ligne envisitant celien <a href="#">Boutique en ligne</a>.[购买] =>Cliquez ici 浇注 acheter en ligne 钢坯[ecodespan] =>S'il vous pla t entrer votre 电子代码[ecodelocdescription] =>Votre code peut tre trouv ci-dessous le code   barres sur votre billet[s3prev] =>客户[s3next] =>前链[验证文本] =>验证电子代码...... Un 即时.))

在这两种情况下,执行 json_encode() 会产生以下结果:

<预><代码>[{语言:fr",语言选择:空,s1next: "Prochain",hascodespan:空,s2prev:空,s2next:空,编码描述:空,购买:Cliquez ici pour acheter en ligne billets",生态跨度:空,编码描述:空,s3prev:空,s3next: "Prochain",验证文本:验证电子代码......非即时."}]

我的数据库设置为 UTF-8,但出于某种奇怪的原因,每次我插入带有法语字符的任何内容时,它都会将自身恢复为某种西欧编码.

主要是,我真的需要 json_encode() 返回有效结果,以便我可以在翻译中使用它.我尝试了 iconv()utf8_encode(),但都无济于事.

任何帮助将非常感激.

解决方案

我设法弄明白了.这不是我真正想要的解决方案,但它有效.我不得不调整我的查询看起来像:

CONVERT(CAST(langselect as BINARY) USING latin1) as langselect

I'm trying to work with French characters. For whatever reason, PHP won't output them correctly unless I force the ISO-8859-1 character set (which I really don't want to do); it won't let me use UTF-8.

So doing a print_r() of my array when I force ISO-8859-1 yields the following:

Array
(
[0] => stdClass Object
    (
        [language] => fr
        [langselect] => î
        [s1next] => Prochain
        [hascodespan] => Avez-vous déjà un code d'enregistrement?
        [s2prev] => Précédent
        [s2next] => Oui, j'ai déjà un code.
        [ecodedescription] => Un E-Code vous donne un accès exclusif à des événements vraiment cool. Si vous êtes dans le besoin d'un code, vous pouvez en acheter un dans notre boutique en ligne en visitant ce lien <a href="#"> boutique en ligne</ a>.
        [purchase] => Cliquez ici pour acheter en ligne billets
        [ecodespan] => S'il vous plaît entrer votre e-code
        [ecodelocdescription] => Votre code peut être trouvé ci-dessous le code à barres sur votre billet
        [s3prev] => Précédent
        [s3next] => Prochain
        [validationtext] => Validation E-Code ... Un instant.
    )

)

When using UTF-8, the output is as following:

Array
(
[0] => stdClass Object
    (
        [language] => fr
        [langselect] => �
        [s1next] => Prochain
        [hascodespan] => Avez-vous d�j� un code d'enregistrement?
        [s2prev] => Pr�c�dent
        [s2next] => Oui, j'ai d�j� un code.
        [ecodedescription] => Un E-Code vous donne un acc�s exclusif � des �v�nements vraiment cool. Si vous �tes dans le besoin d'un code, vous pouvez en acheter un dans notre boutique en ligne en visitant ce lien <a href="#"> boutique en ligne</ a>.
        [purchase] => Cliquez ici pour acheter en ligne billets
        [ecodespan] => S'il vous pla�t entrer votre e-code
        [ecodelocdescription] => Votre code peut �tre trouv� ci-dessous le code � barres sur votre billet
        [s3prev] => Pr�c�dent
        [s3next] => Prochain
        [validationtext] => Validation E-Code ... Un instant.
    )

)

In both instances, executing a json_encode() yields the following result:

[
    {
        language: "fr",
        langselect: null,
        s1next: "Prochain",
        hascodespan: null,
        s2prev: null,
        s2next: null,
        ecodedescription: null,
        purchase: "Cliquez ici pour acheter en ligne billets",
        ecodespan: null,
        ecodelocdescription: null,
        s3prev: null,
        s3next: "Prochain",
        validationtext: "Validation E-Code ... Un instant."
    }
]

I have my database set to UTF-8, yet for some odd reason every time I insert anything with French characters it reverts itself to some Western European encoding.

Mainly, I really need json_encode() to return valid results so I can use it in my translations. I have tried iconv() and utf8_encode(), but to no avail.

Any help would be greatly appreciated.

解决方案

I managed to figure it out. It's not really the solution I wanted but it works. I had to adjust my query to look like:

CONVERT(CAST(langselect as BINARY) USING latin1) as langselect

这篇关于json_encode 不会编码法语字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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