I am able to successfully display foreign characters inside html input fields using htmlspecialchars. But it doesn’t seem to work when I try using the same value in a drop down menu. Any suggestions on what I’m doing wrong?
Until I can get my website redone, it’s currently using charset=iso-8859-1 instead of UTF-8. But like I said, I am able to deal with foreign characters in input fields, so I’m not sure why this is displaying the little question mark icon in place of the accent character in Améo.
I did it as an array so there are a few things you won’t want to take out but hopefully you get the idea. You could create a function to do it instead if you wanted.
But your code doesn’t show us the most important thing - where does $brand come from and what does it contain? The only reasons for incorrect accented characters could be:
Character set mismatch. We know that Améo displays fine in text input fields but we don’t know what $adjustedBrand (coming from $brand) contains. It is very likely that this variable contains text in a different encoding. Check where $brand comes from and check the character encoding of all your php, html and other files, database, etc. - they all need to be in the same character set.
The select box’s font doesn’t have the all the characters you want to display. I’d say this is unlikely but it’s worth mentioning for completeness sake.
This is a workaround that will work but shouldn’t be necessary if the character set of all data is correct.
The most likely reason is wrong database connection character set. Make sure you use mysql_set_charset('latin1'). Also, be ware that (as you probably noticed) it is possible to have a situation where you enter data into the database in a wrong character set (different than the database tables) but the characters still display fine on the web page - this can happen when the database’s character set doesn’t match the one you send the data with - but the data is nevertheless pulled in the correct form as entered. But then the characters will actually be corrupted in the database and as a result some things will not work right, for example sorting and searching. If that is the case then you need to convert the data into the correct character set (for example using iconv) - otherwise you are going to have quite a lot of mess in the future.
You could also have your database in utf8 while the web site is in latin1 (iso) because latin1 is a subset of utf8 - you just need proper character set for the connection. In any case it would be much easier for you for the future to change everything to utf8 right away.