Php learning path: Explanation of iconv functions in php

Source: Internet
Author: User
Php learning path: iconv function description in php iconv function library can complete conversion between various character sets. it is an indispensable basic function library in php programming.

The usage is as follows:

$ String = "dear friend, welcome to Hu Wenfang's blog and hope to help you a little bit! ";

Iconv ("utf8", "gbk", $ string) // Convert string encoding from utf8 to gbk;


The extension is as follows:

Echo $ str = 'hi! welcome to Hu Wenfang's blog, which records the growth of a programmer! ';
Echo'
';
Echo iconv ('gb2312', 'utf-8', $ str); // Encode the string from GB2312 to UTF-8
Echo'
';
Echo iconv_substr ($ str, 1, 1, 'utf-8'); // truncate by number of characters rather than bytes
Print_r (iconv_get_encoding (); // Obtain the encoding information of the current page.
Echo iconv_strlen ($ str, 'utf-8'); // you can specify the length of the encoded string.
// This is also applicable.
$ Content = iconv ("UTF-8", "gbk // transcoder", $ content );

Detailed source reference: http://www.jb51.net/article/14530.htm

Note:

1. iconv is not the default function of php, but also the default installed module. Installation is required.
For Windows + php, you can modify php. ini file, remove ";" Before extension = php_iconv.dll, and copy the iconv under your original php installation file. dll to your winnt/system32 (if your dll points to this directory, I have not performed this step locally)
In linux, you can add one more item -- with-iconv to configure by using static installation. phpinfo shows the iconv items. (Linux7.3 + Apache4.06 + php4.3.2 );

The iconv introduction in php is complete.

2. use the iconv function to convert the captured UTF-8 encoded page to gb2312. it is found that only the iconv function can be used to convert the captured data into less data for no reason. The reason is as follows:

String iconv (string in_charset, string out_charset, string str)
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. If it is truncated, it will be missing;

You can modify iconv ("UTF-8", "GB2312 // IGNORE", $ string) in this way to IGNORE characters that cannot be converted;

Additional:

Introduction to mb_convert_encoding and iconv functions in PHP

The mb_convert_encoding function is used to convert the encoding, which is similar to the iconv function.

Generally, there is no encoding problem in English. this problem occurs only in Chinese data. For example, when you use Zend Studio or Editplus to write a program, you use gbk encoding. if the data needs to be imported into the database and the database is encoded as utf8, you need to encode and convert the data, otherwise, the database will become garbled.

For the usage of mb_convert_encoding, refer to the official website:
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"); // set the character encoding to utp-8
Echo mb_convert_encoding ("?? My friends, UTF-8, GBK ");
?>

Another GB2312 To Big5
<? Php
Header ("content-Type: text/html; charset = big5 ");
Echo mb_convert_encoding ("You are my friend", "big5", "GB2312 ");
?>
However, to use the above functions, you need to install but enable mbstring Extension Library first. The principle is the same. modify php. ini.

Another function iconv in PHP is also used to convert string encoding, similar to the function above.

The following are 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)

Usage:
String mb_convert_encoding (string str, string to_encoding [, mixed from_encoding])
Enable mbstring extension Library first, and remove the extension library before extension = php_mbstring.dll in php. ini.
Mb_convert_encoding can specify multiple input encodings, which are automatically identified based on the content, but the execution efficiency is much lower than iconv. Why does it explain this mb_convert_encoding () function? The answer is as follows:


I found that iconv is converting characters "?" An error occurs when it reaches gb2312. without the ignore parameter, all strings after this character cannot be saved. No matter what, this "?" And cannot be output. In addition, mb_convert_encoding does not have this bug.

Generally, iconv is used. the mb_convert_encoding function is used only when the encoding of the original encoding cannot be determined or The iconv cannot be normally displayed 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.