Replace Special Characters with HTML Codes in PHP

14th July, 2010 in Technical

I always struggle to properly convert special characters into HTML when adding to a web page or importing into a database… the foreign characters with accents like Umlauts etc.
Although PHP has a few functions that claim to do it (e.g. htmlentities() ) , it does not work for all characters I have, just some of them.
So I have written a script to do a find and replace on the characters without using PHP built in functions. This allows me to fine control what characters are converted. If I have missed any I can just add them to the list. It would be great to find a complete list of all characters possible so I don’t have to check content all the time for any not included.

$entities = array(
    ’À’=>’À’,
    ’à’=>’à’,
    ’Á’=>’Á’,
    ’á’=>’á’,
    ’Â’=>’Â’,
    ’â’=>’â’,
    ’Ã’=>’Ã’,
    ’ã’=>’ã’,
    ’Ä’=>’Ä’,
    ’ä’=>’ä’,
    ’Å’=>’Å’,
    ’å’=>’å’,
    ’Æ’=>’Æ’,
    ’æ’=>’æ’,
    ’Ç’=>’Ç’,
    ’ç’=>’ç’,
    ’?’=>’Ð’,
    ’?’=>’ð’,
    ’È’=>’È’,
    ’è’=>’è’,
    ’É’=>’É’,
    ’é’=>’é’,
    ’Ê’=>’Ê’,
    ’ê’=>’ê’,
    ’Ë’=>’Ë’,
    ’ë’=>’ë’,
    ’Ì’=>’Ì’,
    ’ì’=>’ì’,
    ’Í’=>’Í’,
    ’í’=>’í’,
    ’Î’=>’Î’,
    ’î’=>’î’,
    ’Ï’=>’Ï’,
    ’ï’=>’ï’,
    ’Ñ’=>’Ñ’,
    ’ñ’=>’ñ’,
    ’Ò’=>’Ò’,
    ’ò’=>’ò’,
    ’Ó’=>’Ó’,
    ’ó’=>’ó’,
    ’Ô’=>’Ô’,
    ’ô’=>’ô’,
    ’Õ’=>’Õ’,
    ’õ’=>’õ’,
    ’Ö’=>’Ö’,
    ’ö’=>’ö’,
    ’Ø’=>’Ø’,
    ’ø’=>’ø’,
    ’Œ’=>’Œ’,
    ’œ’=>’œ’,
    ’ß’=>’ß’,
    ’?’=>’Þ’,
    ’?’=>’þ’,
    ’Ù’=>’Ù’,
    ’ù’=>’ù’,
    ’Ú’=>’Ú’,
    ’ú’=>’ú’,
    ’Û’=>’Û’,
    ’û’=>’û’,
    ’Ü’=>’Ü’,
    ’ü’=>’ü’,
    ’?’=>’Ý’,
    ’?’=>’ý’,
    ’Ÿ’=>’Ÿ’,
    ’ÿ’=>’ÿ’
);
foreach ($entities as $key => $value)
{
    $ent[] = $key;
    $html_ent[] = $value;
}
$new_string = str_replace( $ent, $html_ent, $string );

Leave a Reply

Recent