Home php教程 PHP开发 php encoding conversion-character encoding conversion

php encoding conversion-character encoding conversion

Dec 15, 2016 am 11:16 AM

Provide a large encoding problem when working with flash effects. Below we provide N kinds of PHP encoding conversion and character encoding conversion functions. Okay, without further ado, just take a look if you like.

The two VB codes above have been converted to UTF-8 (UTF8ENCODEURI) and GB2312 (GBKENCODEURI) respectively.

PRIVATE SUB COMMAND1_CLICK()
DEBUG.PRINT (UTF8ENCODEURI("Chinese character"))
DEBUG.PRINT (GBKENCODEURI("Chinese character"))
END SUB


FUNCTION UTF 8ENCODEURI(SZINPUT)
     DIM WCH, UCH, SZRET
DIM
        FOR X = 1 TO LEN(SZINPUT)

            WCH = MID(SZINPUT, NASC = Ascw (WCH)


if nasc & lt; 0 then nasc = NASC + 65536

IF (NASC and & HFF80) = 0 then

szret & wch

ig
IF (NASC and & HF000) = 0 then
H > CH = "%" & HEX((NASC 2 ^ 12) OR &HE0) & "%" & _

                                                    SZRET = SZRET & UCH

END IF

END IF

    NEXT


iconv() function
Definition and Usage
Definition and usage
iconv() function is to convert the encoding of a string.

Description
string iconv (string in_charset, string out_charset, string str)

Tips and Notes
Notes
Note: In addition to specifying the encoding to be converted to, the second parameter can also add two suffixes: / /TRANSLIT and //IGNORE, where //TRANSLIT will automatically convert characters that cannot be directly converted into one or more approximate characters, //IGNORE will ignore characters that cannot be converted, and the default effect is to start from the first illegal character Truncated.

mb_convert_encoding() function
Definition and Usage
Definition and usage
mb_convert_encoding() function is to convert the encoding of a string.

Description

string mb_convert_encoding (string str, string to-encoding [, mixed from-encoding])

Note: But you need to enable the mbstring extension library first.

The difference between the two: mb_convert_encoding automatically identifies the encoding based on the content; mb_convert_encoding is powerful, but its execution efficiency is much worse than iconv;

Summary: iconv is generally used, only when it is impossible to determine what the original encoding is. Use mb_convert_encoding function.

1. Convert GBK encoded string to UTF-8 encoded string view plaincopy to clipboardprint?
header("content-Type: text/html; charset=Utf-8") ;
echo mb_convert_encoding("You are my good friend", "UTF-8", "GBK");

?>


2. Convert the UTF-8 encoded string to GB2312 encoded string view plaincopy to clipboardprint?
// Pay attention to save this file as a utf-8 encoded format file and then test it
header("content- Type: text/html; charset=gb2312");
echo mb_convert_encoding("You are my good friend", "gb312", "utf-8");
?>


Source: fleaphp.net
The mb_convert_encoding function is PHP's internal multi-byte string encoding conversion function. It can be used in necessary situations (such as solving the problem of garbled Chinese characters caused by using Ajax in the GB2312 encoding environment) to facilitate encoding conversion to solve the problem of garbled web pages. Question, it is very easy to use and very efficient,
supports almost all encodings. PHP 4 >= 4.0.6, PHP 5 versions supported.

Function prototype: Reference:
/**
* Multi-byte string encoding conversion function
*
* @param string str The string that needs to be encoded and converted
* @param string to_encoding Specifies the conversion to a certain encoding, such as : gb2312, gbk, utf-8, etc.
* @param mixed from_encoding mixedly specifies the encoding of the original string, such as: specify JIS, eucjp-win, sjis-win mixed encoding at the same time
* @return string
string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] ) usage examples:

1. Convert GBK encoded string to UTF-8 encoded string view plaincopy to clipboardprint?
header("content-Type: text/html; charset=Utf-8");
echo mb_convert_encoding("You are my good friend", "UTF-8", "GBK");
?>


2. Convert UTF-8 encoded string to GB2312 Encoded string view plaincopy to clipboardprint?
// Pay attention to save this file as a utf-8 encoding format file and then test it
header("content-Type: text/html; charset=gb2312");
echo mb_convert_encoding("You are my good friend", "gb312", "utf-8");
?>

// Please save this file as a utf-8 encoding format file and then test
3. Convert the entire page
This method works in all coding environments. In this way, the character set other than the first 128 characters (display characters) is represented by NCR (Numeric character reference, such as "Chinese characters" will be converted into the form "汉字"). This encoding is The page can be displayed normally in any coding environment.

Add the following three lines of code to the head of the php file: view plaincopy to clipboardprint?
mb_internal_encoding("gb2312"); // gb2312 here is the original encoding of your website
mb_http_output("HTML-ENTITIES");
ob_start('mb_output_handler');

mb_internal_encoding("gb2312"); // gb2312 here is the original encoding of your website mb_http_output("HTML-ENTITIES"); ob_start('mb_output_handler');
Using the mb_convert_encoding function requires enabling PHP mbstring (multi-byte string) extension.

Check the php information page. If the following screen appears:
2008-10-16_111050.png (137.62 KB)
2008-10-16 12:01

It means that mbstring (multi-byte string) extended support has been enabled.

If the above screen does not appear, you need to make the following settings to allow php to support this extension.

1. Windows server environment

Edit the php.ini file, remove the ; in front of extension=php_mbstring.dll, and restart the web server.

2. Linux server environment

Add the --enable-mbstring=cn compilation parameter when compiling the configuration, and then compile and install PHP.

The above is the content of PHP encoding conversion-character encoding conversion. For more related articles, please pay attention to the PHP Chinese website (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 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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 type underline on computer How to type underline on computer Feb 19, 2024 pm 08:36 PM

How to underline on the computer When entering text on the computer, we often need to use underlines to highlight certain content or mark it. However, for some people who are not very familiar with computer input methods, typing underline can be a bit confusing. This article will introduce you to how to underline on your computer. In different computer operating systems and software, the way to enter the underscore may be slightly different. The following will introduce the common methods on Windows operating system and Mac operating system respectively. First, let’s take a look at the operation in Windows

How to solve the problem of garbled characters in tomcat logs? How to solve the problem of garbled characters in tomcat logs? Dec 28, 2023 pm 01:50 PM

What are the methods to solve the problem of garbled tomcat logs? Tomcat is a popular open source JavaServlet container that is widely used to support the deployment and running of JavaWeb applications. However, sometimes garbled characters appear when using Tomcat to record logs, which causes a lot of trouble to developers. This article will introduce several methods to solve the problem of garbled Tomcat logs. Adjust Tomcat's character encoding settings. Tomcat uses ISO-8859-1 character encoding by default.

How to handle character encoding conversion exceptions in Java development How to handle character encoding conversion exceptions in Java development Jul 01, 2023 pm 05:10 PM

How to deal with character encoding conversion exceptions in Java development In Java development, character encoding conversion is a common problem. When we process files, network transmissions, databases, etc., different systems or platforms may use different character encoding methods, causing abnormalities in character parsing and conversion. This article will introduce some common causes and solutions of character encoding conversion exceptions. 1. The basic concept of character encoding. Character encoding is the rules and methods used to convert characters into binary data. Common character encoding methods include AS

PHP Chinese character encoding processing skills sharing PHP Chinese character encoding processing skills sharing Mar 20, 2024 pm 05:12 PM

PHP Chinese character encoding processing skills are shared in Web development, especially when it comes to Chinese character processing, character encoding is often a common problem. Correctly handling Chinese character encoding can avoid problems such as garbled characters and improve the user experience of the website. In PHP, we can use some techniques to handle Chinese character encoding. Below we will share some practical processing methods and code examples. 1. To set the PHP file encoding, you must first ensure that the encoding of the PHP file itself is correct. It is generally recommended to use UTF-8 encoding. in P

How to solve the character encoding problem in Go language How to solve the character encoding problem in Go language Jun 30, 2023 am 09:21 AM

Methods to solve character encoding problems in Go language development In the process of Go language development, character encoding problems are often encountered. Especially when dealing with data input, output, storage and transmission, it is very important to correctly handle character encoding. This article will introduce some methods to solve character encoding problems in Go language development. First, before dealing with the character encoding issue, we need to understand the character encoding standard of the Go language. The Go language uses the Unicode character encoding standard, which is a globally accepted character encoding standard that supports almost

Effective method to solve the problem of garbled characters in the eclipse editor Effective method to solve the problem of garbled characters in the eclipse editor Jan 04, 2024 pm 06:56 PM

An effective method to solve the garbled problem of eclipse requires specific code examples. In recent years, with the rapid development of software development, eclipse, as one of the most popular integrated development environments, has provided convenience and efficiency to many developers. However, you may encounter garbled code problems when using eclipse, which brings trouble to project development and code reading. This article will introduce some effective methods to solve the problem of garbled characters in Eclipse and provide specific code examples. Modify eclipse file encoding settings: in eclip

What is the most commonly used character encoding in computers? What is the most commonly used character encoding in computers? Apr 23, 2020 pm 04:52 PM

The most commonly used character encoding in computers is ASCII code. ASCII is a computer coding system based on the Latin alphabet. It is the most common information exchange standard and is equivalent to the international standard ISO/IEC646.

How to Correctly Handle Chinese Encoding: A Practical Guide to the Go Language How to Correctly Handle Chinese Encoding: A Practical Guide to the Go Language Mar 28, 2024 pm 12:48 PM

How to Correctly Handle Chinese Encoding: A Practical Guide to Go Language With the rapid development of the Internet and computer technology, Chinese encoding has become a problem that cannot be ignored. As a powerful programming language, Go language has certain convenience and flexibility in processing Chinese coding. Correctly handling Chinese encoding is crucial for developers. Today we will discuss how to correctly handle Chinese encoding in the Go language and give some specific code examples. Use UTF-8 encoding When processing Chinese encoding, Go language recommends using UTF

See all articles