PHP Character Set conversion iconv function Use summary _php skill

Source: Internet
Author: User
Tags php programming translit

Iconv function Library can complete the conversion between various character sets, which is an indispensable basic function library in PHP programming.

Usage is as follows:

Copy Code code as follows:

$string = "Welcome to the cloud-dwelling community!" ";
Iconv ("UTF8", "GBK", $string)//converts string strings encoded from UTF8 to GBK;

The extensions are as follows:

Copy Code code as follows:

echo $str = ' Welcome to cloud-dwelling community! ';
echo ' <br/> ';
echo iconv (' GB2312 ', ' UTF-8 ', $str); To transfer the string encoding from GB2312 to UTF-8
echo ' <br/> ';
Echo Iconv_substr ($STR, 1, 1, ' UTF-8 '); Intercept by number of characters rather than bytes
Print_r (Iconv_get_encoding ()); Get current page encoding information
Echo Iconv_strlen ($str, ' UTF-8 '); Gets the string length of the set encoding
That's what it's for.
$content = Iconv ("UTF-8", "Gbk//translit", $content);

Note:

1, Iconv is not the default function of PHP, but also the default installed module. Requires installation to be used.

If it is windows2000+php, you can modify the php.ini file to Extension=php_iconv.dll before the ";" Remove and at the same time you want to copy your original PHP installation file under the Iconv.dll to your winnt/system32 (if your DLL is pointing to this directory, my local does not operate this step)
In the Linux environment, the static installation of the way, in the configure when adding more than one--with-iconv can be, phpinfo see iconv items. (linux7.3+apache4.06+php4.3.2);

Introduction to Iconv in this PHP is complete

2, using the Iconv function to crawl the UTF-8 encoded page into gb2312, found that only the use of the ICONV function to crawl over the data a transcoding data will be less for a reason. The reason is this:

Copy Code code as follows:

String Iconv (String in_charset, String out_charset, String str)

Note: The second parameter, in addition to specifying the encoding to be converted, can add two suffixes://translit and//ignore, where//translit automatically converts characters that cannot be directly converted into one or more approximate characters,//ignore Ignores characters that cannot be converted, and the default effect is to truncate from the first illegal character. Is truncated, of course, will be less;

This can be modified Iconv ("UTF-8", "Gb2312//ignore", $string) will ignore the characters can not be converted;

Additional:

Introduction to Mb_convert_encoding and ICONV functions in PHP

Mb_convert_encoding This function is used to convert the code, and the Iconv function is similar.

English generally does not exist coding problems, only Chinese data will have this problem. For example, when you use Zend Studio or EditPlus to write the program, using the GBK code, if the data needs to enter the database, and the database code for UTF8, then the data will be encoded conversion, or into the database would become garbled.

Mb_convert_encoding's usage See official:

http://cn.php.net/manual/zh/function.mb-convert-encoding.php

Make a GBK to UTF-8

Copy Code code as follows:

<?php
Header ("content-type:text/html; Charset=utf-8 "); The encoding of the set character is Utp-8
Echo mb_convert_encoding ("My", "UTF-8", "GBK");
?>

And a GB2312 to Big5.

Copy Code code as follows:

<?php
Header ("content-type:text/html; Charset=big5 ");
Echo mb_convert_encoding ("friend", "Big5", "GB2312");
?>

However, to use the above functions need to install but need to enable the Mbstring extension library, the reason is the same, modify PHP.ini.
Another function in PHP, Iconv, is also used to convert string encodings, similar to the functions on functions.

Here are a few more examples:

Copy Code code as follows:

Iconv-convert string to requested character encoding
(PHP 4 >= 4.0.5, PHP 5)
Mb_convert_encoding-convert character encoding
(PHP 4 >= 4.0.6, PHP 5)

Usage:

String mb_convert_encoding (String str, string to_encoding [, mixed from_encoding])

Need to enable Mbstring expansion Library, in the php.ini; Extension=php_mbstring.dll in front of; Remove
Mb_convert_encoding can specify multiple input encodings, which are automatically recognized based on content, but perform more efficiently than iconv, why do you want to explain the mb_convert_encoding () function? The answer is as follows:

Iconv found that there was an error in converting the character "-" to gb2312, and if there were no ignore arguments, all the strings after that character could not be saved. In any case, this "-" cannot be converted successfully and cannot be exported. In addition Mb_convert_encoding does not have this bug.

In general, the Mb_convert_encoding function is used only when the ICONV is encountered that cannot determine what encoding the original encoding is, or if the iconv is not displayed properly after conversion.

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.