一千萬個為什麽

搜索

HTML文本字段中的文本編碼

我有一個網站上有一個表格。表單POST到php腳本,然後將數據插入到我的數據庫中。該頁面在<meta>標記中具有charset = UTF-8屬性,並且數據庫設置為使用UTF-8。但是,當我將字符從MS Word復制並粘貼到字段中時,輸出會混亂。

例如,引號中

我正在使用“Microsoft Word”''''

成為

我正在使用“微軟Word”

在數據庫中。

任何人都知道為什麽會這樣?

最佳答案

以下是我建議你去找問題的地方。

  1. MySQL uses charset Latin1 to store and transfer in/out data per default. To change that, do the following. Create your database with charset UTF8/collation utf8_unicode_ci (see http://dev.mysql.com/doc/refman/5.0/en/create-database.html).

    CREATE DATABASE example DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;

  2. Tell MySQL to handle in/out data as UTF8. Before any SQL queries are sent to MySQL the command SET NAMES UTF8; must be made. This tells MySQL to accept and handle all in/out data to the server as UTF8. This needs to be set only once per connection. You can set this with mysql_query("SET NAMES 'UTF8'"); for example.

  3. Make sure you're actually using UTF8. Altough you might have specified UTF8 in the <meta> tag, you might acually be sending the content in another charset. To make sure you're sending UTF8 encoded content, add header('Content-Type: text/html; charset=utf-8'); to your PHP file.

轉載註明原文: HTML文本字段中的文本編碼