0

I am having a problem getting a string comparison to work in PHP if I have "é" in the text. Below is the code that I have with a pile of debug inline code. What the problem is that if I select Montréal in the dropdown the if test doesn't work.

if ($metabox->get_the_value() == $key) echo $selected;
// original code pre debug

Code

a= Array(
    [INFORMATION] => INFORMATIONS
    [LODGING] => HEBERGEMENT
    [LOOK OUT FOR] => À SURVEILLER
    [Montréal] => Montréal
    [PROMOTIONS] ==> PROMOTIONS
    [SECTION NAME] ==> NOM DE SECTION
    [SPOTLIGHT] ==> EN VEDETTE
)

foreach (a as $key => $val){
    echo '<option value="'.$key.'"';
    if (strcasecmp ( trim($metabox->get_the_value())), trim($key))== 0) echo $selected ;
    echo '>'.$key.' ('.$val.'):'. strcmp ( $metabox->get_the_value(),  $key).  '</option>';
}

Output looks like this if I select and save Montréal, it is been saved in the DB.

<select name="_content_language_meta[_content_cat_type]">
    <option value="-1">Select</option>
    <option value="INFORMATION">INFORMATION (INFORMATIONS):Montréal - 1</option>
    <option value="LODGING">LODGING (HEBERGEMENT):Montréal - 1</option>
    <option value="LOOK OUT FOR">LOOK OUT FOR (À SURVEILLER):Montréal - 1</option>
    <option value="Montréal">Montréal (Montréal):Montréal - -1</option>
    <option value="PROMOTIONS">PROMOTIONS (PROMOTIONS):Montréal - -1</option>
    <option value="SECTION NAME">SECTION NAME (NOM DE SECTION):Montréal - -1</option>
    <option value="SPOTLIGHT">SPOTLIGHT (EN VEDETTE):Montréal - -1</option>
</select>

Output if INFORMATION is selected

<select name="_content_language_meta[_content_cat_type]">
    <option value="-1">Select</option>
    <option value="INFORMATION" selected="selected">INFORMATION (INFORMATIONS):INFORMATION - 0</option>
    <option value="LODGING">LODGING (HEBERGEMENT):INFORMATION - -1</option>
    <option value="LOOK OUT FOR">LOOK OUT FOR (À SURVEILLER):INFORMATION - -1</option>
    <option value="Montréal">Montréal (Montréal):INFORMATION - -1</option>
    <option value="PROMOTIONS">PROMOTIONS (PROMOTIONS):INFORMATION - -1</option>
    <option value="SECTION NAME">SECTION NAME (NOM DE SECTION):INFORMATION - -1</option>
    <option value="SPOTLIGHT">SPOTLIGHT (EN VEDETTE):INFORMATION - -1</option>
</select>

This is a WordPress / MYSQL site. Ideas?

dda
  • 6,030
  • 2
  • 25
  • 34
Pbearne
  • 1,025
  • 3
  • 12
  • 25
  • 4
    You should never use `utf8_decode/encode`. [UTF-8 all the way through](http://stackoverflow.com/q/279170/995876) – Esailija Jul 22 '12 at 18:32
  • the utf8_decode was part of some debug code – Pbearne Jul 23 '12 at 12:27
  • 1
    `echo bin2hex($str)` on both strings. What's the result? – deceze Jul 23 '12 at 12:52
  • Montréal (Montréal ):Montréal - 4d6f6e7472266561637574653b616c - 4d6f6e7472c3a9616c – Pbearne Jul 23 '12 at 13:12
  • bin2hex( substr($metabox->get_the_value(),5,1) ).' - '. bin2hex(substr($key,5,1) ) returns "26 - 3c" so its getting formatted converted as it going into the DB and Back :-) – Pbearne Jul 23 '12 at 13:26
  • So I guess I need to convert the string I am test again into the same format ! – Pbearne Jul 23 '12 at 13:27
  • It means you have an encoding problem/mismatch somewhere. [Handling Unicode Front To Back In A Web App](http://kunststube.net/frontback/) – deceze Jul 23 '12 at 13:29
  • .substr( $metabox->get_the_value(),5,1) . ' - '. bin2hex( substr($metabox->get_the_value(),5,1) ).' - '. bin2hex(substr($key,5,1) ). returns & - 26 - c3 and 6 returns :e - 65 - a9 – Pbearne Jul 23 '12 at 13:44
  • I have UTF-8 set in the meta and the in WordPress config (So I hope its setting the DB). Is there a way to tell what the encoding of a string is. Both the values are coming from the DB. looking into the metabox class to see if it is encodeing – Pbearne Jul 23 '12 at 14:13

1 Answers1

0

I am not expert with utf8/char-encoding, but ive had may fair share of problems ..

In regards to how the UTF-8 values are stored, be sure that the database is properly configured to accepts UTF-8 values .. confirm that the utf8 characters are being stored correctly in the DB.

Be sure that the DB character set is set to "utf8" and that the collation is set to some utf8 value as well, for a while I used "utf8_unicode_ci" but recently have changed to use "utf8_bin" as there may be instances where "case-insensitive (ci)" will make queries not behave as one expects (e.g. keY=keY vs key=KeY and key=keY or key=KEY)

farinspace
  • 8,422
  • 6
  • 33
  • 46