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 );
Recent
22nd November, 2017 in
Amity is pleased to announce it's sister web design company has launched in Newport. http://nettlofnewport.co.uk will design & build, host & support and digitally market websites for customers in and around Newport, South Wales, using Amity as the technical backend. Nettl will also offer design & print material for marketing & exhibitions.
Did you know that approximately 96% of people leave a website without taking the action the website owner would like them to complete? Whether it’s buying a product, signing up to a mailing list or requesting a callback, the vast majority of people visiting your site do not do it!
28th November, 2016 in Advice
When did you last check the speed of your website? According to Google’s guide for webmasters, a good eCommerce website should load fully in under 2 seconds...
If your business isn’t currently using social media, now is the time to consider why that is.
25th October, 2016 in Advice, Amity News
December is the perfect time to kick-start campaigns and refresh your marketing. Even if the nature of your business isn’t remotely festive, your customers will appreciate the goodwill of a seasonal offer to two, or simply a friendly ‘Merry Christmas’ message.