Asked  2 Years ago    Answers:  5   Viewed   96 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

1

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
Thursday, September 22, 2022
 
2

I use this:

function replaceSpecial($str){
$chunked = str_split($str,1);
$str = ""; 
foreach($chunked as $chunk){
    $num = ord($chunk);
    // Remove non-ascii & non html characters
    if ($num >= 32 && $num <= 123){
            $str.=$chunk;
    }
}   
return $str;
} 
Friday, October 14, 2022
 
4

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]
Friday, October 14, 2022
 
ahodder
 
5

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.

Monday, November 21, 2022
2

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

Saturday, August 27, 2022
 
leon_
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages