PHP乱码问题,UTF
一.HTML页面转UTF-8编码问题 1.在head后,title前加入一行: meta http-equiv='Content-Type' content='text/html; charset=utf-8' / 顺序不能错,一定要在 显示的标题有可能是乱码! 2.html文件编码问题: 点击编辑器的菜单:“文件”-“另存为”,可以看到
一.HTML页面转UTF-8编码问题
1.在head后,title前加入一行:
1 |
|
顺序不能错,一定要在
显示的标题有可能是乱码!
2.html文件编码问题:
点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8,
如果是ANSI,需要将编码改成:UTF-8。
3.HTML文件头BOM问题:
2.可以用EditPlus打开文件,并在菜单“首选项”->“文件”->"UTF-8标识",设置为:“总是删除签名”,
4.WEB服务器UTF-8编码问题:
如果你按以上所列的步骤做了,还是有中文乱码问题,
请检查你的所使用的WEB服务器的编码问题
如果你使用的是Apache,请将配置文件里的:charset 设成:utf-8(这里仅列出方法,具体格式请参考apache的配置文件)
如果你使用的是Nginx,请将nginx.conf里的:charset 设成 utf-8,
具体找到 "charset gb2312;"或者类似的语句,改成:“charset utf-8;”。
二.PHP页面转UTF-8编码问题
1.在代码开始出加入一行:
1 |
|
2.PHP文件编码问题
点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8,
如果是ANSI,需要将编码改成:UTF-8。
3.PHP文件头BOM问题:
否则,会出现session不能使用的情况,并有类似的提示:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent
2.可以用EditPlus打开文件,并在菜单“首选项”->“文件”->"UTF-8标识",设置为:“总是删除签名”,
4.PHP以附件形式保存文件的时候,UTF-8编码问题:
PHP以附件形式保存文件,文件名必须是GB2312编码,
否则,如果文件名中有中文的话,将是显示乱码:
如果你的PHP本身是UTF-8编码格式的文件,
需要将文件名变量由UTF-8转成GB2312:
iconv("UTF-8", "GB2312", "$filename");
5.截断显示文章标题时,出现乱码或者“?”问号的问题:
一般文章标题很长的时候,会显示一部分标题,会对文章标题进行截断,
由于一个UTF-8编码格式的中文字符会占用3个字符宽度,
截取标题的时候,有时会只截取到一个中文字符的1个字符或2字符宽度,
没截取完整,将出现乱码或“?”问号的情况,
用下面的函数截取标题,就不会有问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
三.MYSQL数据库使用UTF-8编码的问题
1.用phpmyadmin创建数据库和数据表
创建数据库的时候,请将“整理”设置为:“utf8_general_ci”
或执行语句:
1 |
|
创建数据表的时候:如果是该字段是存放中文的话,则需要将“整理”设置为:“utf8_general_ci”,
如果该字段是存放英文或数字的话,默认就可以了。
相应的SQL语句,例如:
1 2 3 4 5 |
|
2.用PHP读写数据库
在连接数据库之后:
[hide]$connection = mysql_connect($host_name, $host_user, $host_pass);
加入两行:
1 2 3 |
|
就可以正常的读写MYSQL数据库了。
四.JS相关的UTF-8编码问题
1.JS读Cookie的中文乱码问题
PHP写cookie的时候需要将中文字符进行escape编码,
否则JS读到cookie中的中文字符将是乱码。
但php本身没有escape函数,我们新写一个escape函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
