詳細闡述PHP環(huán)境下如何將GBK編碼轉(zhuǎn)成UTF-8格式
Jun 08, 2016 pm 05:32 PMapache 字符集 GB 2312
mysql 字符集 GBK
現(xiàn)要從 mysql 中提出信息(GBK) 寫入到 XML 中
XML中的編碼格式是 UTF-8
那么,如何將GBK編碼成 UTF8 ??
具體介紹:
前一陣子見到了qiushuiwuhen君的關(guān)于gbk,unicode,big5的轉(zhuǎn)換的文章
但是多少有一些不太大的問題
于是我設(shè)計了一個負責字符轉(zhuǎn)換的類,修正了其中的一些不足,增加了部分功能,以后我會不斷擴充該類,來支持更多的字符集
增加了如下幾點:
unicode->gbk 符號部分的轉(zhuǎn)換
歐元符的識別
big5,Unicode,GBK之間的相互轉(zhuǎn)換,前提是只轉(zhuǎn)換共同的字符集部分,
使用說明:
暫時程序支持以下字符編碼方式:
GBK,BIG5,UTF-16BE(Unicode big-endian字節(jié)順序),UTF-16LE(Unicode little-endian字節(jié)順序),UTF-8
默認輸入編碼方式為GBK,默認輸出編碼方式為UTF-16BE;
該類別提供兩個函數(shù)用來修改輸入和輸出編碼方式:
修改輸入編碼方式 boolean SetGetEncoding(string $GetEncoding)
修改輸出編碼方式 boolean SetToEncoding(string $ToEncoding)
函數(shù)參數(shù)只能使用上述5種編碼方式,區(qū)分大小寫,如GBK不能寫成gbk
假如設(shè)置成功,返回true,假如使用了錯誤的編碼名,返回false,并現(xiàn)實錯誤信息
函數(shù)
string EncodeString(string $String)
負責進行字符編碼轉(zhuǎn)換,返回轉(zhuǎn)換后的字符串
使用前,請將var $FilePath=""變量該為該程序文件的絕對路徑,否則將會找不到數(shù)據(jù)文件
例子:
將gbk編碼的字符串轉(zhuǎn)化為UTF-8編碼:
$s="GBK編碼";
$CharEncoding=new Encoding();
$CharEncoding->SetGetEncoding("GBK")||die("編碼名錯誤");
$CharEncoding->SetToEncoding("UTF-8")||die("編碼名錯誤");
echo $CharEncoding->EncodeString($s);
在瀏覽器中使用UTF-8編碼察看,將會看到正確的字符

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

In-depth understanding of PHP: Implementation method of converting JSONUnicode to Chinese During development, we often encounter situations where we need to process JSON data, and Unicode encoding in JSON will cause us some problems in some scenarios, especially when Unicode needs to be converted When encoding is converted to Chinese characters. In PHP, there are some methods that can help us achieve this conversion process. A common method will be introduced below and specific code examples will be provided. First, let us first understand the Un in JSON

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

Go language encoding analysis: UTF-8 and GBK comparison In the Go language, processing string encoding is one of the common tasks. Among them, UTF-8 and GBK are two commonly used character encoding methods. This article will conduct a detailed comparison between UTF-8 and GBK, discuss their differences and usage, and attach specific code examples. 1. Introduction to UTF-8 and GBK UTF-8: UTF-8 is a variable-length Unicode encoding method that can represent characters in almost all languages ??in the world. UTF-8

Unicode is a character encoding standard used to represent various languages ??and symbols. To convert Unicode encoding to Chinese characters, you can use Python's built-in functions chr() and ord().

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.
