欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

php 中文乱码的解决方法大集

程序员文章站 2024-01-09 22:12:52
...
??????PHP??ҳ?ı???

1. php?̳??ļ??????ı???????ҳ?ı???Ӧƥ??

a. ??????ʹ??gb2312???룬??ôphpҪ????ͷ??header(??Content-Type: text/html; charset=gb2312")????̬ҳ?????ӣ??????ļ??ı?????ʽΪANSI?????ü??±???????????Ϊѡ??????ΪANSI??????Դ?ļ???

b. ??????ʹ??utf-8???룬??ôphpҪ????ͷ??header(??Content-Type: text/html; charset=utf-8")????̬ҳ?????????????ļ??ı?????ʽΪutf-8??????Ϊutf-8???ܻ??е??鷳??һ??utf-8?ļ???ͷ????BOM??????ʹ??session?ͻ??????⣬????editplus?????棬??editplus?У?????->????ѡ??->?ļ?->UTF-8ǩ????ѡ??????ɾ?????ٱ????Ϳ???ȥ??BOM??Ϣ?ˡ?

2. php????????Unicode?ģ?????substr֮???ĺ????øij?mb_substr????Ҫװmbstring??չ??????????iconvת?롣

???? PHP??Mysql?????ݽ???

PHP?????ݿ??̵̳ı???Ӧһ??

1. ?޸?mysql?̳??????ļ?my.ini??my.cnf??mysql??????utf8????

[mysql]
default-character-set=utf8
[mysqld]
default-character-set=utf8
default-storage-engine=MyISAM
??[mysqld]?¼???:
default-collation=utf8_bin
init_connect='SET NAMES utf8'

2. ????Ҫ?????ݿ???????php????ǰ??mysql_query("set names '????'");????????php????һ?£?????php??????gb2312??mysql????????gb2312????????utf-8??mysql????????utf8????????????????????ʱ?Ͳ?????????????

???? PHP??????ϵͳ????

Windows??Linux?ı????Dz?һ???ģ???Windows?????£?????PHP?ĺ???ʱ??????????utf-8?????????ִ?????????move_uploaded_file()??filesize()??readfile()?ȣ???Щ?????ڴ????ϴ???????ʱ???????õ???????ʱ???ܻ??????????Ĵ???:

Warning: move_uploaded_file()[function.move-uploaded-file]:failed to open stream: Invalid argument in ...

Warning: move_uploaded_file()[function.move-uploaded-file]:Unable to move '' to '' in ...

Warning: filesize() [function.filesize]: stat failed for ... in ...

Warning: readfile() [function.readfile]: failed to open stream: Invalid argument in ..

??Linux????????gb2312??????Ȼ??????????Щ?????????????????ļ??????????뵼???޷???ȡ?ļ?????ʱ???Ƚ?????ת???ɲ???ϵͳʶ???ı??룬????ת??????mb_convert_encoding(?ַ???,?±???,ԭ????)??iconv(ԭ????,?±???,?ַ???)???????????????????ļ????Ͳ??????????룬Ҳ??????????ȡ?ļ???ʵ???????????ļ????ϴ??????ء?

??ʵ???и??õĽ???????????????ϵͳ???룬Ҳ?Ͳ??ÿ???ϵͳ?Ǻα??롣????????һ??ֻ????ĸ?????ֵ???????Ϊ?ļ?????????ԭ?????????ĵ????ֱ????????ݿ??У?????????move_uploaded_file()?Ͳ??????????⣬???ص?ʱ??ֻ?轫?ļ?????Ϊԭ?????????ĵ????֡?ʵ?????صĴ???????

header("Pragma: public");

header("Expires: 0");

header("Cache-Component: must-revalidate, post-check=0, pre-check=0");

header("Content-type: $file_type");

header("Content-Length: $file_size");

header("Content-Disposition: attachment; filename="$file_name"");

header("Content-Transfer-Encoding: binary");

readfile($file_path);

$file_type???ļ??????ͣ?$file_name??ԭ???????֣?$file_path?DZ????ڷ??????ļ??ĵ?ַ


php?ļ??뾲̬??ҳ?ı???Ҫһ?¡?

1.ʹ??utf-8??????ʱ????php?ļ???????????֮ǰ???ϣ?

header(??Content-Type: text/html; charset=utf-8")??
??̬ҳ?????ӣ?
??

?????ļ??ı?????ʽΪutf-8??????Ϊutf-8???ܻ??е??鷳??????WINDOWS?Դ??ļ??±??????????ڱ???һ????UTF-8???????ļ?ʱ???????ļ???ʼ?ĵط????????????ɼ????ַ???0xEF 0xBB 0xBF????BOM——Byte Order Mark????????һ?????ص??ַ????????ü??±??ȱ༭??ʶ???????ļ??Ƿ???UTF-8???롣????һ?????ļ?????????????????ʲô?鷳??

?????? PHP??˵??PHP??????ʱ??û?п???BOM?????⣬????????UTF-8???????ļ???ͷ BOM?????????ַ???????BOM??Ϊ???ļ???ͷ???ĵ?һ???֡????ڱ????? ????EmEditor?????棬??EmEditor?У?????Ϊ->ȥ??unicodeǩ????BOM??ǰ?Ĺ????ٱ????Ϳ???ȥ??BOM??Ϣ?ˡ?

2.ʹ??gb2312???룬php?ļ???????????֮ǰ???ϣ?

header(??Content-Type: text/html; charset=gb2312")??
ҳ??????

?????ļ??ı?????ʽΪANSI??

????PHP?????ݿ??ı???Ӧһ??
??Mysql???ݿ?Ϊ????????Ҫ?????ݿ???????php????ǰ??mysql_query("set names 'xx'");??????php??????gb2312??xx????gb2312????????utf-8??xx???? utf8????utf8 ??????utf-8????????????????ʱ?Ͳ????????????ˡ?

????mysql??????utf8???룬?޸?mysql?????ļ? my.ini??my.cnf

[mysql]
default-character-set=utf8
[mysqld]
default-character-set=utf8
default-storage-engine=MyISAM

?? [mysqld]?¼???:
default-collation=utf8_bin
init_connect='SET NAMES utf8'


echo????????????ʾ?????룬
??ʵӦ???Ǹ??ַ??????ű????????????????⣬
???????DZ??????⣬
һ????˵???ڱ??????ݿ??Ǵ?????ҳ?涼??ҳ???ַ???????Ϊutf-8


??ʱ??Ҫ??????ʾ??????Ҫת??һ?±??뷽ʽ??????
echo iconv("GB2312","UTF-8",'????');?Ͳ?????????
??????????????????
??php??echoǰ??????header("Content-Type:text/html;charset=gb2312");
??Ȼ????????ҳ??Ҳ???Ըɴ??أ?
???е?UTF-8?ij?gb2312


ʵ???????????ֵ???????
?ڱ?????????????????ʾ??ҳ?棬???Ϸ???????echo???????룬
û??ϸ??ĥ??????Ե?ɣ???Ϊͨ??iconv????GB2312??UTF-8????λ?????±????¾??????ˣ?
???????ƿ϶???APACHE????ȷ??˵??PHP?????˵????ò?ͬ???ɵģ?
????PHP.INIӦ?þ??ܽ???

?????ܽ?һ??Ϊʲô??????

һ????˵???????ij?????2??ԭ?????????????ڱ???(charset) ???ô????????????????Դ????ı????????????Ӷ??????????????߰????ġ????顱?????????ļ????Դ????ı?????????Ȼ?????棬????һ???ı??ļ?ԭ????GB2312 ?????ģ?ȴ??UTF-8 ?????????ٱ??档Ҫ???????????????⣬??????Ҫ֪??????????Щ?????漰???˱??룺

1???ļ????룺ָ????ҳ???ļ?(.html,.php??)???????Ժ??ֱ??????????ġ????±???Dreamweaver ?ڴ???ҳ??ʱ?????Զ?ʶ???ļ???????????̫???????⡣??ZendStudioȴ?????Զ?ʶ?????룬??ֻ????????ѡ???????ù̶???ij?ֱ????????ļ???????????ʱ??һ??ע?⣬?ô????????????ļ????????޸?֮??һ???棬?????ͳ????ˣ??????????ᣩ??

2??ҳ?????????룺??HTML????HEAD???棬?????? ??????????????ҳ??????ʲô???룬Ŀǰ??????վ??????XXX??Ҫ?õ???GB2312??UTF-8 ???ֱ??롣

3?????ݿ????ӱ??룺ָ???ǽ??????ݿ?????ʱ???????ֱ????????ݿ⴫?????ݣ???????Ҫע?????Dz?Ҫ?????ݿⱾ???ı?????????????MySQL?ڲ?Ĭ????latin1???룬Ҳ????˵Mysql????latin1???????洢???ݣ??????????봫????Mysql?????ݻᱻת????latin1???롣
֪????WEB????????Щ?ط??漰???˱??룬Ҳ??֪??????????????ԭ????????3?????????ò?һ?£????ڸ??ֱ????????????Ǽ???ASCII?ģ?????Ӣ?ķ??Ų??????֣????ľ͵?ù?ˡ?






$mysql_server_name='localhost';
$mysql_username='root';
$mysql_password='000000';
$mysql_database='lib';
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database);
$sql="select name,age from mytb";
print($conn);
$rs=mysql_db_query("lib","select * from mytb",$conn);
print("

");
while($row = mysql_fetch_object($rs)){
print ($row->name.":".$row->age."
");
}
mysql_close($conn);
?>

??????ʾ???£?

Resource id #1
dd:54
ddd:8
??:15
???:25
??:32
????mysql???룺utf8,GBK???Թ??ˡ?mysql font ??????????ʾ????ȷ??

???????ⲹ?䣺

????????:

???:15
???:25
??:32
?????⼸??,???ݿ?????ֵ?Ǻ???.??ʾ?????????ʺ?.

?????????취??

??????$rs=mysql_db_query("lib","select * from mytb",$conn);

????ǰ??????

????mysql_query("set names gb2312");????mysql_query("set names gbk");


?壮??սһЩ?????Ĵ???????????????

1?????ݿ?????UTF8 ???룬??ҳ????????????GB2312 ??????????IJ?????????ԭ??????ʱ????PHP?ű?????ֱ??SELECT???ݳ????ľ??????룬??Ҫ?ڲ?ѯǰ??ʹ?ã? mysql_query("SET NAMES GBK"); ???趨MYSQL???ӱ??룬??֤ҳ?????????????????趨?????ӱ???һ?£?GBK??GB2312????չ ????????ҳ????UTF-8 ?????Ļ????????ã? mysql_query("SET NAMES UTF8");
ע????UTF8??????һ???õ?UTF-8??????ҳ???????ı????????ݿ??ڲ?????һ?¿??Բ??趨???ӱ??롣

ע????ʵ??MYSQL???????????????????潲?ĸ?????һЩ??MYSQL?????ļ?my.ini?ж?????2??Ĭ?ϱ??룬?ֱ???[client]????default -character-set??[mysqld] ????default-character-set ???ֱ??趨Ĭ??ʱ???ͻ??????Ӻ????ݿ??ڲ??????õı??롣????????ָ???ı?????ʵ??MYSQL?ͻ??????ӷ?????ʱ?????????в???character_set_client????????MYSQL?????????ܵ??Ŀͻ?????????ʲô?????ģ??????Dz???Ĭ?ϱ??롣

2??ҳ?????????????ļ????????벻һ?£????????????ٷ???????Ϊ???????벻һ????????ҳ??ʱ???????????????ľ????????ˡ?????ʱ???Ƿ????Ժ??޸?һЩСBUG???Դ???????????ҳ??Ȼ?????浼?µġ?????????ijЩFTP????ֱ???????޸??ļ???????CuteFTP?????????????????ô?????????ת?????˱??롣

3??һЩ?????????????????ѣ?????????3?????붼??????ȷ?˻????????롣?ȷ?˵??ҳ??GB2312 ?????ģ?IE????????????ȴ????ʶ????UTF-8 ????ҳHEAD?????Ѿ???????GB2312 ?ˣ??ֶ??޸???????????ΪGB2312 ??ҳ????ʾ??????????ԭ???Ƿ?????Apache?趨?˷?????ȫ?ֵ?Ĭ?ϱ??룬??httpd.conf????????AddDefaultCharset UTF-8 ????ʱ?????????????ȷ???HTTPͷ???????????????ȼ???ҳ?????????????ߣ???Ȼ????????ʶ?????ˡ??????취??2??????????Ա???????ļ??Լ?????????????һ??AddDefaultCharset GB2312 ??????ȫ?????ã????????Լ?Ŀ¼??.htaccess?????á?