Special Characters to HTML Entities

Sep 8 2011 12:41 PM SEP 8 2011 12:41 PM
JavaScript character entitiesJavascript

Recently I posted about HTMLEntities and a PHP function that can convert crazy characters and such to their HTML form.

Along with this I posted about encoding types. Let me first say that these two go hand and hand. When dealing with funky characters such as "éœ∑´®†¥ˆË™ƒ." And then throw in different page encoding's such as UTF-8 and ISO-8859-1. If you take those same characters and POST them or output them from a database to a different encoding type you might soon find them to be entirely different.

Frustratingly enough there is tons of methods to fix this issue, but found only one method to work for my situation. It basically involves changing the characters to their HTML variants before being saved to the database.

My situation involves a wiziwig editor that then uses an ajax call to save the data in that editor to a MySQL database. From that point the user will review their entry and then publish this information to another database. The wiziwig editor was on a UTF-8 site and the text then being published to an ISO-8859-1. Unfortunately I was not able to change the ISO-8859-1 to UTF-8 as it has many other text that relies heavily on the ISO-8859-1.

My solution that I found text-symbols.com  a script that basically converts alt codes and special charaters to their hexidecimal html alternates. This solved my problem as the browser can then determine which encoding type to use by the header. And from that conver the HTML characters to the proper character.

text-symbols.com solution includes a little more than I needed so I cut it down to the following:

function hexentity(d) {
    if(d.charCodeAt(0) > 127 && d != '<' && d != '>')
        return '&#x' + d.charCodeAt(0).toString(16) + ';'; else return d;
}
function htmlEntity(input){
    var bb = '';
    for (i = 0; i < input.length; i++) bb += hexentity(input.charAt(i));
    return bb;
}

Use just throw your string into htmlEntity( str ) and your done.