Home Web Front-end HTML Tutorial Summary of UTF-8 encoding problems encountered in web development

Summary of UTF-8 encoding problems encountered in web development

Dec 16, 2016 pm 03:45 PM

There are mainly five aspects:

1. The problem of converting HTML pages to UTF-8 encoding

2. The problem of converting PHP pages to UTF-8 encoding

3. The problem of using UTF-8 encoding for MYSQL databases

4. JS Related UTF-8 encoding issues

5. FLASH related UTF-8 encoding issues

1. HTML page conversion to UTF-8 encoding issues

1. Add a line after and before : </p> <p><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /></p> <p>The order cannot be wrong, and must be added before the <title> tag, otherwise if there are Chinese characters between <title> and , the displayed title may be garbled!

2.html file encoding problem:

Click the menu of the editor: "File"->"Save As", you can see the encoding of the current file, make sure the file encoding is: UTF-8, if it is ANSI, it is required Change the encoding to: UTF-8.

3. HTML file header BOM problem:

When converting files from other encodings to UTF-8 encoding, sometimes a BOM tag is added at the beginning of the file. The BOM tag may cause the browser to Garbled characters appear when displaying Chinese characters.

How to delete this BOM tag:

1. You can open the file with Dreamweaver and resave it to remove the BOM tag!

2. You can open the file with EditPlus, and in the menu "Preferences"->"File"->"UTF-8 Identity", set it to: "Always delete signatures", then save the file, and you can remove it. BOM tag!

4. WEB server UTF-8 encoding problem:

If you follow the steps listed above and still have Chinese garbled problems, please check the encoding problem of the WEB server you are using

If you are using Apache, please set the charset in the configuration file to: utf-8 (only the methods are listed here, please refer to the apache configuration file for the specific format).

If you are using Nginx, please set: charset in nginx.conf to utf-8, specifically find "charset " gb2312;" or similar statement, change it to: "charset utf-8;".



2. PHP page conversion to UTF-8 encoding problem

1. Add a line at the beginning of the code:

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

2.PHP file encoding problem

Click the menu of the editor: "File"->"Save As", you can see Go to the encoding of the current file and make sure the file encoding is: UTF-8. If it is ANSI, you need to change the encoding to: UTF-8.

3. PHP file header BOM problem:

PHP files must not have BOM tags. Otherwise, there will be a situation where the session cannot be used, and there will be a similar prompt:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

This is because, while executing session_start() When , the entire page cannot be output, but because the BOM tag exists in the previous PHP page, PHP treats the BOM tag as output, so an error occurs!

So the BOM tag must be deleted on the PHP page

How to delete this BOM tag:

1. You can open the file with Dreamweaver and resave it to remove the BOM tag!

2. You can open the file with EditPlus, and in the menu "Preferences"->"File"->"UTF-8 Identity", set it to: "Always delete signatures", then save the file, and you can remove it. BOM tag!

4. UTF-8 encoding problem when PHP saves a file as an attachment:

When PHP saves a file as an attachment, the file name must be GB2312 encoded. Otherwise, if there is Chinese in the file name, it will be garbled:

If your PHP itself is a file in UTF-8 encoding format, you need to convert the file name variable from UTF-8 to GB2312:

iconv("UTF-8", "GB2312", "$filename");

5. When truncating and displaying the article title, garbled characters or "?" question marks appear:

Generally, when the article title is very long, part of the title will be displayed, and the article title will be truncated, because a UTF-8 encoded Chinese The characters will occupy 3 characters in width. When intercepting the title, sometimes only 1 character or 2 characters in width of a Chinese character will be intercepted. If the interception is not complete, garbled characters or "?" question marks will appear. Use the following function to intercept Title, there will be no problem:

function get_brief_str($str, $max_length) 
{ 
echo strlen($str) 
."<br>"; 
if(strlen($str) > $max_length) 
{ 
$check_num = 0; 
for($i=0; $i < $max_length; $i++) 
{ 
if (ord($str[$i]) > 128) 
$check_num++; 
} 

if($check_num % 3 == 0) 
$str = substr($str, 
0, $max_length)."..."; 
else if($check_num % 3 == 1) 
$str = substr($str, 
0, $max_length + 2)."..."; 
else if($check_num % 3 == 2) 
$str = 
substr($str, 0, $max_length + 1)."..."; 
} 

return $str; 
}
Copy after login

3. Problems with using UTF-8 encoding for MYSQL database

1. Use phpmyadmin to create databases and data tables

When creating a database, please set "Organization" to: "utf8_general_ci ” or execute the statement:

CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Copy after login

When creating a data table: If the field stores Chinese, you need to set “Organization” to: “utf8_general_ci”,
If the field stores English or numbers, the default is fine .

The corresponding SQL statement, for example:

CREATE TABLE `test` ( 
`id` INT NOT NULL , 
`name` VARCHAR( 10 ) CHARACTER 
SET utf8 COLLATE utf8_general_ci NOT NULL , 
PRIMARY KEY ( `id` ) 
) 
ENGINE = MYISAM ;
Copy after login

2. Use PHP to read and write the database

After connecting to the database:

$connection = mysql_connect($host_name, $host_user, $host_pass);
Copy after login

Add two lines:

mysql_query("set character set &#39;utf8&#39;");//读库 
mysql_query("set names &#39;utf8&#39;");//写库
Copy after login

Then you can read and write the MYSQL database normally.


4. UTF-8 encoding issues related to JS

1. Chinese garbled problem when JS reads cookies

PHP needs to escape encode Chinese characters when writing cookies, otherwise JS reads Chinese characters in cookies will be gibberish.

但php本身没有escape函数,我们新写一个escape函数:

function escape($str) 
{ 
preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r); 
$ar = $r[0]; 
foreach($ar as $k=>$v) 
{ 
if(ord($v[0]) < 128) 
$ar[$k] = 
rawurlencode($v); 
else 
$ar[$k] = 
"%u".bin2hex(iconv("UTF-8","UCS-2",$v)); 
} 
return join("",$ar); 
}
Copy after login

JS读cookie的时候,用unescape解码,然后就解决cookie中有中文乱码的问题了。

2.外部JS文件UTF-8编码问题

当一个HTML页面或则PHP页面包含一个外部的JS文件时,如果HTML页面或则PHP页面是UTF-8编码格式的文件,外部的JS文件同样要转成UTF-8的文件,否则将出现,没有包含不成功,调用函数时没有反应的情况。

点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8,如果是ANSI,需要将编码改成:UTF-8。



五.FLASH相关的UTF-8编码问题

FLASH内部对所有字符串,默认都是以UTF-8处理

1.FLASH读文普通本文件(txt,html)

要将文本文件的编码存为UTF-8

点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8,如果是ANSI,需要将编码改成:UTF-8。

2.FLASH读XML文件

要将XML文件的编码存为UTF-8

点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8,如果是ANSI,需要将编码改成:UTF-8。

在XML第1行写:

<?xml version="1.0" encoding="utf-8"?>
Copy after login


3.FLASH读PHP返回数据

如果PHP编码本身是UTF-8的,直接echo就可以了。

如果PHP编码本身是GB2312的,可以将PHP转存成UTF-8编码格式的文件,直接echo就可以了

如果PHP编码本身是GB2312的,而且不允许改文件的编码格式,用下面的语句将字符串转换成UTF-8的编码格式

$new_str = iconv("GB2312", "UTF-8", "$str");
Copy after login
Copy after login

再echo就可以了

4.FLASH读数据库(MYSQL)的数据

FLASH要通过PHP读取数据库中的数据,PHP本身的编码不重要,关键是如果数据库的编码是GB2312的话,需要用下面的语句将字符串转换成UTF-8的编码格式。

$new_str = iconv("GB2312", "UTF-8", "$str");
Copy after login
Copy after login

5.FLASH通过PHP写数据

一句话,FLASH传过来的字符串是UTF-8格式的,要转换成相应的编码格式,再操作(写文件、写数据库、直接显示等等),还是用iconv函数转换。

6.FLASH使用本地编码(理论上不推荐使用)

如果想让FLASH不使用UTF-8编码,而是使用本地编码。对于中国大陆地区而言,本地编码是GB2312或GBK

AS程序内,可以添加以下代码:

System.useCodepage = true;
Copy after login

那么FLASH内所有字符都是使用GB2312的编码了,所有导入到FLASH或者从FLASH导出的数据,都应该做相应的编码转换。

因为使用本地编码,会造成使用繁体中文地区的用户产生乱码,所以不推荐使用。

 以上就是Web 开发中遇到的UTF-8编码的问题总结的内容,更多相关文章请关注PHP中文网(www.php.cn)! 


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use python+Flask to realize real-time update and display of logs on web pages How to use python+Flask to realize real-time update and display of logs on web pages May 17, 2023 am 11:07 AM

1. Log output to file using module: logging can generate a custom level log, and can output the log to a specified path. Log level: debug (debug log) = 5) {clearTimeout (time) // If all results obtained 10 consecutive times are empty Log clearing scheduled task}return}if(data.log_type==2){//If a new log is obtained for(i=0;i

How to use Nginx web server caddy How to use Nginx web server caddy May 30, 2023 pm 12:19 PM

Introduction to Caddy Caddy is a powerful and highly scalable web server that currently has 38K+ stars on Github. Caddy is written in Go language and can be used for static resource hosting and reverse proxy. Caddy has the following main features: Compared with the complex configuration of Nginx, its original Caddyfile configuration is very simple; it can dynamically modify the configuration through the AdminAPI it provides; it supports automated HTTPS configuration by default, and can automatically apply for HTTPS certificates and configure it; it can be expanded to data Tens of thousands of sites; can be executed anywhere with no additional dependencies; written in Go language, memory safety is more guaranteed. First of all, we install it directly in CentO

Real-time protection against face-blocking barrages on the web (based on machine learning) Real-time protection against face-blocking barrages on the web (based on machine learning) Jun 10, 2023 pm 01:03 PM

Face-blocking barrage means that a large number of barrages float by without blocking the person in the video, making it look like they are floating from behind the person. Machine learning has been popular for several years, but many people don’t know that these capabilities can also be run in browsers. This article introduces the practical optimization process in video barrages. At the end of the article, it lists some applicable scenarios for this solution, hoping to open it up. Some ideas. mediapipeDemo (https://google.github.io/mediapipe/) demonstrates the mainstream implementation principle of face-blocking barrage on-demand up upload. The server background calculation extracts the portrait area in the video screen, and converts it into svg storage while the client plays the video. Download svg from the server and combine it with barrage, portrait

How to configure nginx to ensure that the frps server and web share port 80 How to configure nginx to ensure that the frps server and web share port 80 Jun 03, 2023 am 08:19 AM

First of all, you will have a doubt, what is frp? Simply put, frp is an intranet penetration tool. After configuring the client, you can access the intranet through the server. Now my server has used nginx as the website, and there is only one port 80. So what should I do if the FRP server also wants to use port 80? After querying, this can be achieved by using nginx's reverse proxy. To add: frps is the server, frpc is the client. Step 1: Modify the nginx.conf configuration file in the server and add the following parameters to http{} in nginx.conf, server{listen80

How to implement form validation for web applications using Golang How to implement form validation for web applications using Golang Jun 24, 2023 am 09:08 AM

Form validation is a very important link in web application development. It can check the validity of the data before submitting the form data to avoid security vulnerabilities and data errors in the application. Form validation for web applications can be easily implemented using Golang. This article will introduce how to use Golang to implement form validation for web applications. 1. Basic elements of form validation Before introducing how to implement form validation, we need to know what the basic elements of form validation are. Form elements: form elements are

Using Jetty7 for Web server processing in Java API development Using Jetty7 for Web server processing in Java API development Jun 18, 2023 am 10:42 AM

Using Jetty7 for Web Server Processing in JavaAPI Development With the development of the Internet, the Web server has become the core part of application development and is also the focus of many enterprises. In order to meet the growing business needs, many developers choose to use Jetty for web server development, and its flexibility and scalability are widely recognized. This article will introduce how to use Jetty7 in JavaAPI development for We

Is PHP front-end or back-end in web development? Is PHP front-end or back-end in web development? Mar 24, 2024 pm 02:18 PM

PHP belongs to the backend in web development. PHP is a server-side scripting language, mainly used to process server-side logic and generate dynamic web content. Compared with front-end technology, PHP is more used for back-end operations such as interacting with databases, processing user requests, and generating page content. Next, specific code examples will be used to illustrate the application of PHP in back-end development. First, let's look at a simple PHP code example for connecting to a database and querying data:

How to enable administrative access from the cockpit web UI How to enable administrative access from the cockpit web UI Mar 20, 2024 pm 06:56 PM

Cockpit is a web-based graphical interface for Linux servers. It is mainly intended to make managing Linux servers easier for new/expert users. In this article, we will discuss Cockpit access modes and how to switch administrative access to Cockpit from CockpitWebUI. Content Topics: Cockpit Entry Modes Finding the Current Cockpit Access Mode Enable Administrative Access for Cockpit from CockpitWebUI Disabling Administrative Access for Cockpit from CockpitWebUI Conclusion Cockpit Entry Modes The cockpit has two access modes: Restricted Access: This is the default for the cockpit access mode. In this access mode you cannot access the web user from the cockpit

See all articles