Home Backend Development PHP Tutorial Chinese PHP programming under UTF8_PHP tutorial

Chinese PHP programming under UTF8_PHP tutorial

Jul 13, 2016 pm 05:33 PM
php utf8 Down Chinese Preface Same screen yes show of programming

前言:

说实话,
凉鞋也觉得 UTF8 是好东西……
毕竟同屏显示中日韩对东亚人的吸引力是不小的……
(当然好处不仅是这点啦……)
不仅是网页程式……
很多应用程式的内核都开始使用 Unicode 编码……
目的是显而易见的:支援多语言显示……
微软的所有软体都是 Unicode 内核……
所以日文软体拿到你的中文 XP 上是可以正常显示的……
而中文 98 就会因为 GB 内核安装其他语言软体时造成乱码……

至于 UTF8 ,
可以说是 Unicode 的一个分支,
它用三个字节保存一个汉字……
(Unicode 用四个字节)
应用软体都集体投奔 Unicode 了……
就不允许我们搞网页程式的用 UTF8?

本文中凉鞋就尽量多方位介绍一下 UTF8 编码下的 php(做为现在的主流开发语言) 编程……
至于为什么单独介绍“中文”……
一来是因为英文这玩意实在不需要考虑 UTF8 ……
除非你准备做多语言系统……
(我要发些牢骚:现在的老外写程式时根本不重视这个问题……)
二来是中日韩等多字节语系在 UTF8 编码下的处理方式其实大同小异……
依葫芦画瓢即可……
好……先从数据库处理部分开始吧……


==========================================
连接数据库

很多人刚升级到 MySQL(和PHP搭配之最佳组合) 4.1 时会发现数据乱掉了……
其实是因为 MySQL(和PHP搭配之最佳组合) 从 4.1 开始支援字符集了……
而且默认字符集正是 UTF8 ……
(充分证明与国际接轨的重要性…… 嘿嘿……)
而以前我们大多使用 utf8 或是 GBK 编码……
这样以来输出的数据当然是乱码……
要解决乱码……
就得让程序知道该获取什么编码的数据……

我们假设你以前的数据库是 utf8 编码的……
那么你可以在查询前添加一句

MySQL(和PHP搭配之最佳组合)_query(SET CHARACTER SET utf8) or die("Query failed : " . MySQL(和PHP搭配之最佳组合)_error());
当然,由于 4.1 以上才需要这样处理,
因此我们可以加上判断:

$MySQL(和PHP搭配之最佳组合)version = $db->query_first("SELECT VERSION() AS version");
if ($MySQL(和PHP搭配之最佳组合)version[version] >= 4.1)
{
MySQL(和PHP搭配之最佳组合)_query(SET CHARACTER SET utf8) or die("Query failed : " . MySQL(和PHP搭配之最佳组合)_error());
}
这样以来不管 MySQL(和PHP搭配之最佳组合) 默认编码是什么都可以正常存取了……
(不论您是存活期,还是存定期,甚至是零存整取都没有问题鸟……)

但是,人家都国际化鸟……
您还在用 utf8 行么?
如何转码呢?
还有……
数据升级时出现乱码怎么办?
凉拌!
且听下回分解……

============================================
数据升级至 4.1

要升级……
就得先导出……
要说老外还真不负责……
以前的导出方式总是弄丢一些中文字符……
比如把“我爱你娘”弄成“我爱你”啦……
(通常是丢失一段数据最末尾的字)
整个儿差了一辈儿……
(用石榴姐的话说就是“这么大逆不道的事实在是太刺激了”……)
为了保护您脆弱的心脏……
也为了维护中国传统伦理道义……
您可以把数据包含中文字符的字段改为二进制(Binary)编码……
具体方法嘛……
可以运行这个语句:

ALTER TABLE `表名` CONVERT TO CHARACTER SET binary;
这样,那些字符类型字段,如:
CHAR、VARCHAR 和 TEXT
将转换为
BINARY、VARBINARY 和 BLOB
然后再导出并导入到 4.1 环境中……
当然,最后一项繁琐的工作是:
你需要把它们的类型再改回来……

有往 4.1 升级的……
当然也有往下降级的……
怎么降级???
凉鞋去上个厕所……
而您请翻下页……

=============================================
数据从 4.1 降级

有人发现从4.1导出的 SQL 文件无法导入低版本程序……
问题其实很简单……
而且 MySQL(和PHP搭配之最佳组合) 已经为我们想好了一切……
导出时请添加 –compatible 参数……
我们假设您的数据库是 utf8 编码的……
而且目标数据库版本为 4.0 ……
那么命令行下这么写:

shell>MySQL(The best combination with PHP)dump --user=username --password=password --compatible=MySQL(The best combination with PHP)40 --default-character-set=utf8 database > db.sql
The exported SQL file can be successfully imported into the lower version database...

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508616.htmlTechArticlePreface: To be honest, Sandals also thinks UTF8 is a good thing... After all, China, Japan and South Korea are displayed on the same screen against East Asians The attraction is not small... (Of course, the benefits are not limited to this...) Not only...
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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Demystifying C: A Clear and Simple Path for New Programmers Demystifying C: A Clear and Simple Path for New Programmers Oct 11, 2024 pm 10:47 PM

C is an ideal choice for beginners to learn system programming. It contains the following components: header files, functions and main functions. A simple C program that can print "HelloWorld" needs a header file containing the standard input/output function declaration and uses the printf function in the main function to print. C programs can be compiled and run by using the GCC compiler. After you master the basics, you can move on to topics such as data types, functions, arrays, and file handling to become a proficient C programmer.

See all articles