Search code examples
phpunicodemeta

UTF code in database when displayed on page, after providing meta, is still in UTF form


I have an UTF code for single quotes inside my database, but when trying to display it on the page, it's still in UTF code for some reason, even though I provided the meta tags. What am I doing wrong?

In Database: '

On Page: '

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

Solution

  • In the case of apostrophe or double quotes, it may be necessary to be able to include such characters without danger of closing an attribute. But you should be concerned about why the database has the data stored in this manner in the first place. If some script is doing the escaping, it may be mistakenly doing that twice. Double-escaping will not be harmful, but it may turn some harmless escapes added by users (e.g., if they want to use < or &) into these visible codes.

    Sometimes such escaping may be a sign that all special characters are being escaped (not just apostrophes) for the sake of security (e.g., to escape < into &lt; so that no tags can be added with user inputted data), but double-escaping (as in your example) is probably a sign that the input has been entered into the database incorrectly. If you convert &amp; back to &, this should at least not be a security problem for HTML though, but it could be a security problem for genuine XML.

    Also, FYI, the proper term instead of "UTF code" is Unicode numeric character references. "UTF-8", "UTF-16", etc. are different ways to encode abstract Unicode characters, but numeric character references provide a way to avoid characters getting corrupted in programs that do not properly support Unicode, and they also make visible some invisible, or difficult to see, characters.