Php encoding and conversion-character encoding and conversion

Source: Internet
Author: User

It provides a big problem in encoding with flash effect. Below we provide N php encoding and character encoding conversion functions. Let's take a look at it if you don't like it much.

It provides a big problem in encoding with flash effect. Below we provide N php encoding and character encoding conversion functions. Let's take a look at it if you don't like it much.

The two sections of VB code are respectively converted to UTF-8 (UTF8ENCODEURI) and GB2312 (GBKENCODEURI.

Private sub commandementclick ()
DEBUG. PRINT (UTF8ENCODEURI ("Chinese character "))
DEBUG. PRINT (GBKENCODEURI ("Chinese character "))
END SUB


FUNCTION UTF8ENCODEURI (SZINPUT)
Dim wch, UCH, SZRET
DIM X
Dim nasc, NASC2, NASC3

If szinput = "" THEN
UTF8ENCODEURI = SZINPUT
EXIT FUNCTION
END IF

For x = 1 to len (SZINPUT)
WCH = MID (SZINPUT, X, 1)
NASC = ASCW (WCH)

If nasc <0 then nasc = NASC ++ 65536

IF (nasc and & HFF80) = 0 THEN
SZRET = SZRET & WCH
ELSE
IF (nasc and & HF000) = 0 THEN
UCH = "%" & HEX (NASC 2 ^ 6) OR & HC0) & HEX (nasc and & H3F OR & H80)
SZRET = SZRET & UCH
ELSE
UCH = "%" & HEX (NASC 2 ^ 12) OR & HE0) & "% "&_
HEX (NASC 2 ^ 6) AND & H3F OR & H80) & "% "&_
HEX (nasc and & H3F OR & H80)
SZRET = SZRET & UCH
END IF
END IF
NEXT


Iconv () function
Definition and Usage
Definition and usage
The iconv () function is used 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, you can also add two suffixes: // Transcoder and // IGNORE, // Transcoder automatically converts a character that cannot be directly converted into one or more similar characters. // IGNORE ignores the characters that cannot be converted, by default, the result is truncated from the first invalid character.

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

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

Note: however, enable mbstring extension library is required first.
Differences between the two: Automatic Identification and encoding based on content in mb_convert_encoding; the function of mb_convert_encoding is powerful, but the execution efficiency is much lower than that of iconv;

Summary: Generally, iconv is used. The mb_convert_encoding function is used only when it is difficult to determine the encoding type of the original encoding.

1. Convert the GBK encoding string to the UTF-8 encoding string view plaincopy to clipboardprint?
<? Php
Header ("content-Type: text/html; charset = Utf-8 ");
Echo mb_convert_encoding ("You are my good friend", "UTF-8", "GBK ");
?>


2, The UTF-8 encoding string into GB2312 encoding string view plaincopy to clipboardprint?
// Remember to save the file to a UTF-8 encoded file before testing
<? Php
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 a multi-Byte Character String Conversion Function in php. It can be used as needed (for example, to solve the problem of text garbled by Ajax in the GB2312 encoding environment) it facilitates Code Conversion to solve the problem of garbled web pages. It is very convenient to use and efficient,
Almost all encodings are supported. PHP 4> = 4.0.6 and PHP 5.

Function prototype: Reference:
/**
* Multi-byte string encoding conversion function
*
* @ Param string str the string to be encoded and converted
* @ Param string to_encoding specifies the conversion to a certain encoding, such as gb2312, gbk, and UTF-8.
* @ Param mixed from_encoding: specify both the encoding of the original string, for example, JIS, eucjp-win, and sjis-win.
* @ Return string
String mb_convert_encoding (string str, string to_encoding [, mixed from_encoding]) Example:

1. Convert the GBK encoding string to the UTF-8 encoding string view plaincopy to clipboardprint?
<? Php
Header ("content-Type: text/html; charset = Utf-8 ");
Echo mb_convert_encoding ("You are my good friend", "UTF-8", "GBK ");
?>

<? Php header ("content-Type: text/html; charset = Utf-8"); echo mb_convert_encoding ("you are my friend", "UTF-8", "GBK");?>
2, The UTF-8 encoding string into GB2312 encoding string view plaincopy to clipboardprint?
// Remember to save the file to a UTF-8 encoded file before testing
<? Php
Header ("content-Type: text/html; charset = gb2312 ");
Echo mb_convert_encoding ("You are my good friend", "gb312", "UTF-8 ");
?>

// Remember to save the file into a UTF-8 encoded file before testing <? Php header ("content-Type: text/html; charset = gb2312"); echo mb_convert_encoding ("You are my good friend", "gb312", "UTF-8");?>
3. Convert the entire page
This method applies to all encoding environments. In this way, the character sets other than the first 128 characters (display characters) are all in the form of NCR (Numeric character reference, for example, "Chinese character" will be converted to "& #27721; & #23383) in any encoding environment.

Add the following three lines of code to the header of the PHP file: view plaincopy to clipboardprint?
Mb_internal_encoding ("gb2312"); // here, gb2312 is the original code of your website.
Mb_http_output ("HTML-ENTITIES ");
Ob_start ('mb _ output_handler ');

Mb_internal_encoding ("gb2312"); // here gb2312 is your website's original code mb_http_output ("HTML-ENTITIES"); ob_start ('mb _ output_handler ');
To use the mb_convert_encoding function, you must enable the mbstring (multi-byte string) Extension of PHP.

View the php information page. If the following screen is displayed:
2008-10-16_111050.png (137.62 KB)

Mbstring (multi-byte string) extension is enabled.

If the above image 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, and replace the file before extension = php_mbstring.dll. Remove the file and restart the web server.

2. Linux server environment

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.