PHP Learning Path: PHP in Iconv function detailed explanation

Source: Internet
Author: User
Tags translit

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

use methods such as the following :

$string = "Dear friend, Welcome to Hu Wenfang's blog. Hope to bring you a little help! ";

Iconv ("UTF8", "GBK", $string)//Converts string encoding from UTF8 to GBK.


Extensions such as the following:

echo $str = ' Hello, welcome to Hu Wenfang's blog, which records a program Ape's growth process! ';
Echo '
‘;
echo iconv (' GB2312 ', ' UTF-8 ', $str); To transfer the encoding of a string from GB2312 to UTF-8
Echo '
‘;
Echo Iconv_substr ($STR, 1, 1, ' UTF-8 '); Intercept by number of characters rather than bytes
Print_r (Iconv_get_encoding ()); Get the current page encoding information
Echo Iconv_strlen ($str, ' UTF-8 '); Gets the string length of the set encoding
That's what it's used for.
$content = Iconv ("UTF-8", "Gbk//translit", $content);

Specific references: http://www.jb51.net/article/14530.htm

Note:

1, Iconv is not the default PHP function, is also the default installation of the module.

Need to install talent.
Suppose is windows2000+php, you can change the php.ini file, will extension=php_iconv.dll before the ";" Remove, at the same time you want to copy your original PHP installation file under the Iconv.dll to your winnt/system32 (assuming your DLL is pointing to this folder, my local do not operate this step )
In a Linux environment, with static installation, add one more--with-iconv to configure. Phpinfo See the Iconv item. (linux7.3+apache4.06+php4.3.2);

The Iconv Introduction to this PHP is complete

2, using the Iconv function to crawl over the Utf-8 encoded page into gb2312, found that only practical iconv function to crawl over the data of a transcoding data will be less for no reason.

The reason is this:

String Iconv (String in_charset, String out_charset, String str)
Note: The second number of parameters. In addition to being able to specify the encoding to be converted to. You can also add two suffixes://translit and//ignore, in which//translit will voluntarily convert characters that cannot be converted directly 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;

The ability to change Iconv ("UTF-8", "Gb2312//ignore", $string ) ignores characters that cannot be converted;

Additional:

Introduction to Mb_convert_encoding and ICONV functions in PHP

Mb_convert_encoding This function is used to convert the encoding. Almost identical to the Iconv function.

In general, there is no coding problem in English, only Chinese data will have this problem. For example, when you use Zend Studio or EditPlus to tap code. With the GBK code, assuming that the data need to enter the database, and the database is encoded as UTF8, then the data will be encoded to convert, or into the database will become garbled.

Mb_convert_encoding the use of the method see the official:
http://cn.php.net/manual/zh/function.mb-convert-encoding.php

Make a GBK to UTF-8
< PHP
Header ("content-type:text/html; Charset=utf-8 "); The encoding of the set character is Utp-8
Echo mb_convert_encoding ("You Are my Friend", "UTF-8", "GBK");
?

>

One more GB2312 to Big5.
< PHP
Header ("content-type:text/html; Charset=big5 ");
Echo mb_convert_encoding ("You Are my Friend", "Big5", "GB2312");
?>
Just want to use the above function need to install but need to first enable mbstring extension library, the same reason. Change the php.ini.

Another function in PHP, Iconv, is also used to convert string encodings. Similar to functions on the upper function.

Below are some other specific examples:
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)

How to use:
String mb_convert_encoding (String str, string to_encoding [, mixed from_encoding])
You need to enable the Mbstring extension library first. In the php.ini will; Extension=php_mbstring.dll in front of; Remove
Mb_convert_encoding can specify a variety of input code, it will be based on the content of their own initiative to identify, but the efficiency of the operation than Iconv too much, then why explain the mb_convert_encoding () function? The answers are as follows:


Iconv is found to have an error converting the character "-" to gb2312, assuming there are no ignore parameters. All strings After this character cannot be saved.

No matter what. This "-" cannot be converted successfully and cannot be exported.

Another mb_convert_encoding does not have this bug.

Under normal circumstances with ICONV, only when encountered can not determine the original encoding is what encoding, or iconv conversion can not be normal show only mb_convert_encoding function.

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

PHP Learning Path: PHP in Iconv function detailed explanation

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.