Home  >  Article  >  php教程  >  php encoding conversion-character encoding conversion

php encoding conversion-character encoding conversion

黄舟
黄舟Original
2016-12-15 11:16:172312browse

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:
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