Nowadays I am working on an application in WordPress and got many issues regarding character Encoding especially for German characters like (ä,ü,ë) and the ones that are coming from the database. It happened in all the browsers. I checked my browser and its character Encoding is UTF-8 and those characters are ISO-8859-1 so I have added the meta tag mentioned below:
1 2 3 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> |
And also have added the following line in my htaccess file :
1 2 3 | AddDefaultCharset ISO-8859-1 |
After doing that still, the characters have some problem and it doesn’t look proper. I found that ä,ü,ë etc are the ISO-8859-1 so if I change my browser Encoding to ISO-8859-1, it works fine but whenever I change browser to UTF-8 , it doesn’t work.
Finally, I have created one function which converts string for encoding characters and solved the character problems too:
1 2 3 4 5 6 7 8 9 10 | function makeStringSecure ($string) { $string = trim($string); $string = strip_tags($string); $string = htmlentities($string, ENT_NOQUOTES); $string = stripslashes($string); $string = mysql_real_escape_string($string); return $string; } |
Recommended Read
Clean HTML content in PHP
Convert RGB to Hex HTML code in PHP
Get English Ordinal Number Suffix in PHP
Let’s have a look with one simple example
1 2 3 4 5 6 7 8 9 10 | $conn = mysql_connect("localhost","root",""); $db = mysql_select_db("test"); $res = mysql_query("insert into category (cat_name) VALUES ('".makeStringSecure('Musikalität')."')"); $line = mysql_query("select * from category"); $row = mysql_fetch_assoc($line); echo $row['cat_name']; // Prints Musikalität /**in any browser**/ |
Thanks for reading this article :). I hope this solution helpful to you. If you have any concern, please share your thoughts on the comment section.
Comments (2)