I am trying to use a database to store a set of LONGTEXT which I input into the database using a form. But when I put characters such as é they come out of the database as �, is there any way of converting all accented characters to HTML entities using PHP before it is sent off to the database?
I am aware of htmlentities (); but this also replaces characters such as < which I do not want as it does not load the HTML when the content is echoed onto the page.
Thanks in Advance
Whenever you see things such as
�, that is a sign you have a charset mismatch.If you have your database and html all set in UTF-8 encoding, you will not have an issue. Chances are, either your HTML or Database is set to use Latin-1 (ISO 8859-1) and the other UTF-8 or vise-versa.
You can solve this by unifying on a single characterset. I personally prefer UTF-8, since it is universal. However, if you are unable do that, you can use PHP to covert between UTF-8 and Latin-1 using the following functions.
UTF-8 to Latin-1:
Latin-1 to UTF-8:
If you encode your data before sending it to the database and decode it before displaying, or vise-versa, depending on your database/html configuration, your problem will be solved.
As another alternative, if you have the PHP iconv extension available on your server, you can use that to accurately transliterate or convert to plain ASCII.
http://www.php.net/manual/en/function.iconv.php