完美解决PHP中文乱码的问题

PHP中文乱码一般是字符集问题,编码主要有下面几个问题。

一.首先是PHP网页的编码

1. php文件本身的编码与网页的编码应匹配

a. 如果欲使用gb2312编码,那么php要输出头:header(“Content-Type: text/html; charset=gb2312"),静态页面添加<meta http-equiv="Content-Type" content="text/html; charset=gb2312">,所有文件的编码格式为ANSI,可用记事本打开,另存为选择编码为ANSI,覆盖源文件。

b. 如果欲使用utf-8编码,那么php要输出头:header(“Content-Type: text/html; charset=utf-8"),静态页面添加<meta http-equiv="Content-Type" content="text/html; charset=utf-8">,所有文件的编码格式为utf-8。保存为utf-8可能会有点麻烦,一般utf-8文件开头会有BOM,如果使用session就会出问题,可用editplus来保存,在editplus中,工具->参数选择->文件->UTF-8签名,选择总是删除,再保存就可以去掉BOM信息了。

2. php本身不是Unicode的,所有substr之类的函数得改成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的编码是不一样的,在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是保存在服务上文件的地址。

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » 完美解决PHP中文乱码的问题

152 评论

  1. buy olmesartan 20mg generic buy divalproex diamox online

  2. order medroxyprogesterone without prescription order periactin generic buy periactin 4mg without prescription

  3. order provigil generic modafinil 200mg ca ivermectin 3 mg tablet dosage

  4. viagra price viagra ca lisinopril 2.5mg pills

  5. buy prilosec without prescription cheap essay help hard rock casino online

  6. order clomiphene 50mg generic free roulette blackjack poker online

  7. cialis generic name us viagra sildenafil 100mg cheap

  8. purchase tretinoin generic avanafil pill avanafil 200mg generic

  9. naproxen order naproxen order order prevacid 15mg without prescription

  10. tadalafil liquid play casino casino online slots

  11. play for real online casino games gambling casino casino near me

  12. order ramipril 10mg sale amaryl price buy arcoxia 120mg sale

  13. doxycycline online order cleocin 300mg price order cleocin 150mg without prescription

  14. price of ivermectin prednisone oral order prednisone 5mg online cheap

  15. generic uroxatral trazodone uk diltiazem 180mg without prescription

  16. Пребывание каждого человека в постоянной опасности и осознание бессилия против природы объединили
    людей в общество. А созданию
    системы наказаний предшествовал запрет
    на убийство одного индивида другим и наличие такой возможности убийства у общества в случае нарушения названного запрета.
    Как пробудить интерес у мужчины советы психолога

  17. buy femara 2.5 mg without prescription cheap generic viagra viagra overnight shipping

  18. is cialis generic cialis order otc ed pills that work

  19. order doxycycline 100mg for sale albuterol 4mg uk augmentin 375mg pills

  20. lisinopril 5mg pill cost prilosec 10mg order metoprolol 100mg online cheap

  21. buy generic hyzaar for sale purchase nexium pill buy topiramate 200mg without prescription

  22. amoxicillin over the counter anastrozole pills order clarithromycin 250mg online cheap

  23. order catapres 0.1 mg online meclizine 25mg ca where to buy spiriva without a prescription

  24. azipro where to buy order azipro brand gabapentin 800mg

  25. nifedipine 30mg over the counter order fexofenadine brand fexofenadine 120mg

  26. order dapoxetine online cheap generic priligy 60mg order orlistat 60mg without prescription

  27. ketorolac drug inderal tablet buy inderal 10mg generic

  28. buy ed pills without a prescription order tadalafil cialis 5mg without prescription

  29. cheap frumil 5 mg clindac a us buy cheap zovirax

评论已关闭

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡