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?




