標籤:
聲明:本篇文章來自http://www.jb51.net/article/30123.htm
PHP頁面轉UTF-8編碼問題
1.在代碼開始出加入一行:
header("Content-Type: text/html;charset=utf-8");
<span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">2.PHP檔案編碼問題 </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">點擊編輯器的菜單:“檔案”->“另存新檔”,可以看到當前檔案的編碼,確保檔案編碼為:UTF-8, </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">如果是ANSI,需要將編碼改成:UTF-8。 </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">3.PHP檔案頭BOM問題: </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">PHP檔案一定不可以有BOM標籤 </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">否則,會出現session不能使用的情況,並有類似的提示: </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">這是因為,在執行session_start() 的時候,整個頁面不能有輸出,但是當由於前PHP頁面存在BOM標籤, </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">PHP把這個BOM標籤當成是輸出了,所以就出錯了! </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">所以PHP頁面一定要刪除BOM標籤 </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">刪除這個BOM標籤的方法: </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">1.可以用Dreamweaver開啟檔案,並重新儲存,即可以去除BOM標籤! </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">2.可以用EditPlus開啟檔案,並在菜單“喜好設定”->“檔案”->"UTF-8標識",設定為:“總是刪除簽名”, </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">然後儲存檔案,即可以去除BOM標籤! </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">4.PHP以附件形式儲存檔案的時候,UTF-8編碼問題: </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">PHP以附件形式儲存檔案,檔案名稱必須是GB2312編碼, </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">否則,如果檔案名稱中有中文的話,將是顯示亂碼: </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">如果你的PHP本身是UTF-8編碼格式的檔案, </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">需要將檔案名稱變數由UTF-8轉成GB2312: </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">iconv("UTF-8", "GB2312", "$filename"); </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">利用程式來執行個體字元截取方法 </span>
<span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"></span><pre name="code" class="php">function utf8_substr($str,$len) { for($i=0;$i<$len;$i++) { $temp_str=substr($str,0,1); if(ord($temp_str) > 127){ $i++; if($i<$len){ $new_str[]=substr($str,0,3); $str=substr($str,3); } }else { $new_str[]=substr($str,0,1); $str=substr($str,1); } } return join($new_str); }
MYSQL資料庫使用UTF-8編碼的問題
1.用phpmyadmin建立資料庫和資料表
建立資料庫的時候,請將“整理”設定為:“utf8_general_ci”
或執行語句:
<span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"></span><pre name="code" class="php">CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
<span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">建立資料表的時候:如果是該欄位是存放中文的話,則需要將“整理”設定為:“utf8_general_ci”, </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">如果該欄位是存放英文或數位話,預設就可以了。 </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">相應的SQL語句,例如: </span>
<span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"></span><pre name="code" class="sql">CREATE TABLE `test` ( `id` INT NOT NULL , `name` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM ;
2.用PHP讀寫資料庫
在串連資料庫之後:
<span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"></span><pre name="code" class="php">$connection = mysql_connect($host_name, $host_user, $host_pass);
加入兩行:
<span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"></span></span><pre name="code" class="php">mysql_query("set character set 'utf8'");//讀庫 mysql_query("set names 'utf8'");//寫庫 //其實讀寫都可以只加入mysql_query("set names 'utf8'")
就可以正常的讀寫MYSQL資料庫了。
用的appserv-win32-2.5.10做的環境,裝這個包的時候用預設的utf8編碼。
在寫資料庫連接檔案時,寫成:
<span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"></span></span></span><pre name="code" class="php">$conn = mysql_connect("$host","$user","$password"); mysql_query("SET NAMES 'UTF8'"); mysql_select_db("$database",$conn);
然後在做頁面時,注意這句:
<span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px; background-color: rgb(221, 237, 251);"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </span></span></span></span>
<span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">這樣不管輸入資料庫的中文,還是頁面顯示,就都正常了。 </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">在DW CS4版裡,預設產生的也是utf8頁面。 </span><br style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;" /><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;">同樣的,如果一開始寫資料庫連接檔案時寫成: </span></span></span></span><pre name="code" class="php">mysql_query("SET NAMES 'GBK'");
那頁面也要相應變成:
<span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"></span></span></span><pre name="code" class="cpp"><meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<span style="font-family: tahoma, arial, 宋體; font-size: 14px; line-height: 25.2px;"></span>
PHP寫入資料庫中文亂碼問題