HTML character encoding with PHP made easy
Supposing you've got a string (called $mystring) that has a lot of unusual characters in it, it'd be helpful to convert them all to their HTML entities. I stumbled across this when dealing with forms where users would enter content in their native format (Croatian, Czech, etc) and it needed to be HTML encoded for database reasons. Clearly doing a character-by-character str_replace would be problematic so you need a way to convert them all.
$mystring = mb_convert_encoding($mystring, 'HTML-ENTITIES', 'UTF-8');
So we have $mystring that has a lot of nasty characters in it, by running it through the above function it converts all UTF-8 characters into their HTML entities. Which can be quite helpful.
Enjoy this article? Why not subscribe to the full RSS feed?





Showing most recent 1 of 1 comments
Far better to store you data in the database in its native characters... Obviously you would still make the data safe by means of something like mysql_real_escape_string().
mb_convert_encoding($mystring, ’HTML-ENTITIES’, ’UTF-8’) would do a lot more than make the string database safe. Would it in fact do that? Would it deal with single quotes?
I would suggest that this function is used when you output your data to an HTML page. But even then if you send a UTF-8 header to the browser it will do all the magic for you and display the characters correctly.