I have a php script generating form mail for clients to use in their web site , so now how to take and saving input text to the database in the utf-8 encoding regardless of any character set that client use in their web html form site . I am a newbie at php . thanks for any help on this matter.
As for using UTF-8 in PHP You need to use the multibyte string functions instead of the old string functions, here is a list http://php.net/manual/en/function.mb-stripos.php For preg_ functions you need to set the /u switch for the expression. It can also be helpful to call these 3 commands at the start of you php script to set it to use utf-8 internally as well, but this isn't generally needed mb_language("uni"); mb_detect_order("UTF-8, ISO-8859-1"); mb_internal_encoding('UTF-8'); And most importantly, save your php files in utf-8 encoding (methods differs between editor) The html part of it: Originaly it was the intent that the header should specify the text encoding but as this wasn't practical for the html author to change and especially not per file browsers today support a meta tag, it looks like this. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> With that in the header your browser should detect the site as using utf-8. In php it's pretty easy to also sent the header as well if you want it to be by the book. Just include header('Content-Type: text/html; charset=utf-8'); at the top of the file. There are also a couple of pit falls here when you save you php file in utf-8 (usualy the editor will display this option in the save as dialog) you should avoid adding, as this will prevent php from overwriting header information and can show up as two odd symbols. If you are using a mysql database you need to set encoding (what encoding it uses to talk to php in), this is done by sending these command in php right after you call mysql_select_db() or mysql_connect() mysql_query("SET session character_set_results = 'UTF8'"); mysql_query("SET session character_set_client = 'UTF8'"); mysql_query("SET session character_set_connection = 'UTF8'"); mysql_query("SET session character_set_results = 'UTF8'"); mysql_query("SET session character_set_server = 'UTF8'"); Setting the encoding of the database in mysql can be done when creating or editing a database/table using phpmyadmin if you use mysql 5+. older versions of mysql do not use a specific encoding but should work as long as when editing the content via phpmyadmin (set encoding to utf8 under lang settings). You can also convert the text to what ever encoding you prefer to use, using either iconv(), mb_convert_encoding() or utf8_decode(). This isn't a quick way to get around all the above, you should still make sure each of them are set to the encoding you choose for you application, and there is a good reason to choose UTF-8. If you are still having issues after this (or during this) I recommend that you test the text integrity each step of the way so that you can better see what part of the chain is using the wrong encoding or converting it incorrectly (check browser text encoding and javascript alert before submitting. echo from php and check browser text encoding, echo before and after submitting to database. extract from database, echo and check browser text encoding). So just to summarize: step 1: Save files in UTF-8 step 2: Add meta tag to html indicating encoding step 3: Set MYSQL session character encoding step 4: Set database encoding Goodluck
Thanks for your comment. the scripts use the server default character set as is UTF-8, and the generated code page's header meta is: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> generated code by scripts is copied and copy paste to their web site by users , although the header meta character set is not included with the code, user will use any character set they wish to use(shift_jis- euc-jp- or whatever), and is not controled by me. I am not familiar with the mysql query , is there any other way that I can go for this. Thank you