Take the 2-minute tour ×

I'm having an issue that i believe is related to unicode text. When the user enters a string that has the unicode bullet character, mysql is not able to save that field (the rest of the update query works though). Here's how i've been trying to deal with it.

$str = "· Close up the server";

$str = preg_replace("\u2022", "&bull;", $str);

...however this is still not working.

share|improve this question
Did you try $str = preg_replace("·", "&bull;", $str); ? –  Vladimir Dec 20 '12 at 17:04
Did you try $str = htmlentities($str)? –  GolezTrol Dec 20 '12 at 17:05
@Vladimir has a point –  samayo Dec 20 '12 at 17:05
Tell us about your database's collation/encoding. Suggested reading: joelonsoftware.com/articles/Unicode.html –  ficuscr Dec 20 '12 at 17:06
Table encoding: cp1252 Western European (latin 1) Collation: latin1_swedish_ci –  drschultz Dec 20 '12 at 17:15

2 Answers 2

up vote 3 down vote accepted

So many things can go wrong here, because database, form submits and source code string literals are all involved. I'll assume you want to use UTF-8, because with any other typical encoding (CP1252, Latin1) you'll be screwed when you want to use json_ or accept more than ~200 different characters.

The first thing to do is remove any kind of conversion etc code that was written with the intention of trying to fix encoding issues. Such as utf8_encode, htmlentitites, *_replace.. whatever.

Source encoding.

$str = "· Close up the server";

When writing the above, the PHP source file needs to be physically encoded in UTF-8. If you are on Windows, you must explicitly do or configure this. UTF-8 doesn't happen magically on Windows.

Form submits

When user submits a form, the payload will be in whatever encoding you declared the page to be. You can declare it like so:


But anyone can actually submit arbitrary bytes to your server, so you should validate the input is in UTF-8 before proceeding. mb_check_encoding is good.


Since at this point your data is coming in as UTF-8, your input strings are in UTF-8. You must specify this after connecting to the database, by specifying a connection encoding.

mysql_set_charset("utf8"); //After making the connection, and before any queries
//or $mysqli->set_charset( "utf8");

This makes the database read your input in UTF-8, and encode its output in UTF-8. You would also want to set your columns/tables/databases to UTF-8 as well.

Unicode escape sequences \uxxxx or \uhhhh\ullll or \Uxxxxxxxx are not supported in PHP.

share|improve this answer

\u2022 is the UTF-16 hex encoding for "Bullet". Not UTF-8.

You might also want to SET NAMES 'UTF-8'; or change charset before you open your database.

share|improve this answer
Up-voting this, down-voting OP's question. Question shows a lack of understanding unicode and character encoding. String manipulation or iconv is not the answer. It will just break on the next "Gremlin" character Windows decides to map to a control character. –  ficuscr Dec 20 '12 at 17:35

Your Answer


