Asked  7 Months ago    Answers:  5   Viewed   31 times

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.

 Answers

43

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
Wednesday, March 31, 2021
 
Viralk
answered 7 Months ago
65

The eclipse browser probably just doesn't know which encoding to use, since you don't send any HTTP header specifying it.

Try sending the following HTTP header: Content-Type: text/html; charset=utf-8

Or use a real browser, and set the page encoding to UTF-8 explicitely.

Wednesday, March 31, 2021
 
Fernando
answered 7 Months ago
14

If you are using all sort of characters and accented letters, then why don't just allow anything to go by?

NOTE: Using this Rule will also allow spaces;

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)-(d+).htm$   classified.php?id=$2 [L]

And later if you decide to limit the regex, lets say you don't want any of these characters # $ % then you have to make exceptions:

   Options +FollowSymlinks
  RewriteEngine on
  RewriteRule ^([^#$%]+)-(d+).htm$   classified.php?id=$2 [L]
Saturday, May 29, 2021
 
Anax
answered 5 Months ago
80

I figured it out if anyone is in need of a similar piece of help:

I changed this line:

$fieldenquiry = utf8_encode($_POST['fieldenquiry']);

To this:

$fieldenquiry = utf8_encode(htmlentities($_POST['fieldenquiry'], ENT_QUOTES, "UTF-8"));

I use the htmlentities() function with UTF-8 specified in the arguments.

This fixed the issue completely. Hope it helps someone.

Saturday, May 29, 2021
 
Anax
answered 5 Months ago
62

This is an encoding issue. It looks like at some point, the data gets represented as ISO-8859-1.

Every part of your process needs to be UTF-8 encoded.

  • The database connection

  • The database tables

  • Your PHP file (if you are using special characters inside that file as shown in your example above)

  • The content-type headers that you output

Wednesday, June 23, 2021
 
Juriy
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :