What would be the best way to determine the encoding type in PHP from the MySQL database character set?
-
According to https://dev.mysql.com/doc/refman/5.5/en/charset-charsets.html, these are the supported character sets by MySQL. Are there other character sets that aren't listed? big5 dec8 cp850 hp8 koi8r latin1 latin2 swe7 ascii ujis sjis hebrew tis620 euckr koi8u gb2312 greek cp1250 gbk latin5 armscii8 utf8 ucs2 cp866 keybcs2 macce macroman cp852 latin7 utf8mb4 cp1251 utf16 cp1256 cp1257 utf32 binary geostd8 cp932 eucjpms According to http://www.php.net/manual/en/mbstring.supported-encodings.php, the following list is the supported encodings by PHP. UCS-4* UCS-4BE UCS-4LE* UCS-2 UCS-2BE UCS-2LE UTF-32* UTF-32BE* UTF-32LE* UTF-16* UTF-16BE* UTF-16LE* UTF-7 UTF7-IMAP UTF-8* ASCII* EUC-JP* SJIS* eucJP-win* SJIS-win* ISO-2022-JP ISO-2022-JP-MS CP932 CP51932 SJIS-mac** (alias: MacJapanese) SJIS-Mobile#DOCOMO** (alias: SJIS-DOCOMO) SJIS-Mobile#KDDI** (alias: SJIS-KDDI) SJIS-Mobile#SOFTBANK** (alias: SJIS-SOFTBANK) UTF-8-Mobile#DOCOMO** (alias: UTF-8-DOCOMO) UTF-8-Mobile#KDDI-A** UTF-8-Mobile#KDDI-B** (alias: UTF-8-KDDI) UTF-8-Mobile#SOFTBANK** (alias: UTF-8-SOFTBANK) ISO-2022-JP-MOBILE#KDDI** (alias: ISO-2022-JP-KDDI) JIS JIS-ms CP50220 CP50220raw CP50221 CP50222 ISO-8859-1* ISO-8859-2* ISO-8859-3* ISO-8859-4* ISO-8859-5* ISO-8859-6* ISO-8859-7* ISO-8859-8* ISO-8859-9* ISO-8859-10* ISO-8859-13* ISO-8859-14* ISO-8859-15* byte2be byte2le byte4be byte4le BASE64 HTML-ENTITIES 7bit 8bit EUC-CN* CP936 GB18030** HZ EUC-TW* CP950 BIG-5* EUC-KR* UHC (CP949) ISO-2022-KR Windows-1251 (CP1251) Windows-1252 (CP1252) CP866 (IBM866) KOI8-R* Now I'd like to specify the PHP character encoding based on the MySQL character set that the user uses. What would be the best way to do it?
-
Answer:
PHP has a lot of encoding issues owing to its simple assumption that each character equals 1 byte, which makes it highly incompatible with UTF-8, you can read more about it here - http://www.phpwact.org/php/i18n/charsets If you are using PHP-Mysqli extension, there is a function http://www.php.net/manual/en/mysqli.character-set-name.php, which will give you the current character set. Now, you can use string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding ] ) to convert it to a character set of your choice.
Deepanshu Mehndiratta at Quora Visit the source
Other answers
As per my experience its tough to work on multilingual support, btw "I ⥠Unicode" :-) To start on unicode make sure about following things... each element may have it's own encoding. PHP files (text editor) PHP internal Output to client Client browser Input from client (get, post, cookies, files) MySQL storage MySQL link/client inputs/outputs or components ( like copy paste from MS word file in CMS tool) If you ensure that you have did descend job on above points then you can start work on unicode. Few major points are: Always use PHP mb_string APIs like ... mb_substr,mb_internal_encoding('UTF-8') set proper header header('Content-Type: text/html; charset=UTF-8'); Mysql: mysql_query("SET NAMES 'utf8';", $link); mysql_query("SET CHARACTER SET 'utf8';", $link); ALTER DATABASE mydatabase CHARACTER SET utf8 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT COLLATE utf8_general_ci ; ALTER TABLE mytable DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ; Universal Unicode support is long battle .. so take decisions carefully .. Reference Links: http://www.intertwingly.net/blog/1761.html http://hsivonen.iki.fi/php-utf8/ http://en.wikipedia.org/wiki/UTF-8#Description http://www.lookuptables.com/
Ravi Raj
Related Q & A:
- What is the best way to distribute an audio/video feed from a computer to TVs over existing indoor coax cable?Best solution by Audio-Video Production
- What would be the best way to ship a wii?Best solution by Yahoo! Answers
- What is the best amp for a 12" Alpine Type R?Best solution by Yahoo! Answers
- What is the very best way to get into the insurance business?Best solution by Yahoo! Answers
- What is the best way to learn PHP?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.