abstract:在我們每個(gè)人在開始學(xué)習(xí)php的時(shí)候,都經(jīng)歷過(guò)一個(gè)痛苦的亂碼過(guò)程,手足無(wú)措?,F(xiàn)在告訴大家四種方法,徹底避免亂碼。 第一類:文件亂碼 沒(méi)出指定編碼, 但目前瀏覽器有的是默認(rèn)gbk,有的卻是默認(rèn)是utf8,這就導(dǎo)致亂碼!
在我們每個(gè)人在開始學(xué)習(xí)php的時(shí)候,都經(jīng)歷過(guò)一個(gè)痛苦的亂碼過(guò)程,手足無(wú)措?,F(xiàn)在告訴大家四種方法,徹底避免亂碼。
第一類:文件亂碼
沒(méi)出指定編碼, 但目前瀏覽器有的是默認(rèn)gbk,有的卻是默認(rèn)是utf8,這就導(dǎo)致亂碼!
解決辦法:
1、 入口文件的第一行 ,強(qiáng)制輸出(常用)!
<?php header("Content-type: text/html; charst=utf-8");
2、 html模版head部分(常用)!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head>
第二類 數(shù)據(jù)庫(kù)亂碼
1、數(shù)據(jù)庫(kù)本身創(chuàng)建的時(shí)候指定編碼(常用)
create table cz_category( cat_id smallint unsigned not null auto_increment primary key comment '商品類別ID', cat_name varchar(30) not null default '' comment '商品類別名稱', parent_id smallint unsigned not null default 0 comment '商品類別父ID', cat_desc varchar(255) not null default '' comment '商品類別描述', sort_order tinyint not null default 50 comment '排序依據(jù)', unit varchar(15) not null default '' comment '單位', is_show tinyint not null default 1 comment '是否顯示,默認(rèn)顯示', index pid(parent_id) )engine=MyISAM charset=utf8;
如果是在phpmyadmin下創(chuàng)建的,那么就應(yīng)該
2、 php連接mysql(常用)
<?php function getField($table_name) { $link = mysql_connect('localhost','root','1234abcd'); mysql_query('use daxue'); mysql_query('set names utf8'); $result=mysql_query("desc {$table_name}");
更多關(guān)于PHP如何解決亂碼的方法請(qǐng)關(guān)注PHP中文網(wǎng)(m.miracleart.cn)其它文章!