2

i have some data with special characters saved on a table of my database (e.g. ç)

When i need to print that data i use the php function htmlentities and i add this in my page head

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

All works without problem... BUT if i print that data into an option of a dynamic menù it doesn't work.

Here is the code:

<select name="group" id="group" style="width:220px">   
  <option value="0">Choose a Group</option>   
  <?php  
  $ls_groups = ...; // calling function of a class
  foreach ($ls_groups as $dett){
    echo "<option value=" . $dett->id . " " . ($dett->id == $myGroup ? "selected" : "") .  ">" . htmlentities($dett->description,ENT_QUOTES, "UTF-8")  . "</option>";
  }
  ?>  
</select>

Where i can search the problem?

Thanks!

Edit:

This is what i take only if printed in the options Image

  • 2
    What do you mean by "doesn't work"? Please specify exactly what goes wrong and perhaps give an example. – 3472948 Feb 20 '17 at 10:38
  • Also, check if your PHP file is UTF8 encoded. Common mistake if you use special character – Pavel Janicek Feb 20 '17 at 10:40
  • **nvisser** Thanks for reply, added an image! **PavelJanicek** yeah, just for say it works everywhere instead of in the options! **Pete** i have 2 servers, config of php.ini & httpd are equals, on one it work, on another nothing! Any idea? –  Feb 20 '17 at 10:42
  • With other languages than English it is always detective work. Can you please show how you call `$ls_groups` variable? There can be problem. Also check [iconv](http://php.net/manual/en/function.iconv.php) function – Pavel Janicek Feb 20 '17 at 11:01
  • no problems in $ls_groups if i save the "htmlentities" into a var and print it after (out of the options) it works. Also iconv doesn't work :( –  Feb 20 '17 at 11:17
  • Then please paste the code what it does when $ls_groups is filled in. The error will most probably be either in that function or in difference between printing it out in the option and "outside" of it – Pavel Janicek Feb 20 '17 at 12:17
  • Also show how it prints out without any error - what **exact** code do you use. Without these informations, it is hard to help you – Pavel Janicek Feb 20 '17 at 12:18
  • $ls_groups hasn't any role... if i change that part in a direct assignment the problem persist. `htmlentities($dett->description,ENT_QUOTES, "UTF-8")` out of the option it work, if i put in option it fails. Just for explain `$ls_groups = $class->query() ` and return an array of object –  Feb 20 '17 at 12:24
  • @Luca do you realize that you are the one in need of help? Please do not make my guesswork even harder than it is and let me help you. There must be error in your code and most probably it is in the parts you are reluctant to show. If you do not want to show your code, then do not expect to be helped – Pavel Janicek Feb 20 '17 at 12:50
  • Also, while at it, check character set of your database – Pavel Janicek Feb 20 '17 at 12:53
  • @PavelJanicek i understand you are here for help but... if character set of db is the problem it always fail... in the $ls_groups there is a query in the class and i can't post it... i had removed all the part of $ls_groups and assigned the value of data in a variable and maked the code so: `echo "";` still not working –  Feb 20 '17 at 13:41

1 Answers1

0

Try htmlspecialchars() or set charset 'ISO-8859-1'

Reference htmlentities() vs. htmlspecialchars()

Community
  • 1
  • 1
Phantom
  • 377
  • 4
  • 10