PHP iconv Function Knowledge Summary, Phpiconv function Summary _php tutorial

Source: Internet
Author: User
Tags translit

Iconv function Knowledge Summary in PHP, Phpiconv function Summary

Today, when revising the paper online, I met the Iconv function. Learn a bit

Header (' Content-type:application/;charset=utf-8 '); $name =iconv (' utf-8 ', ' gb2312 ', $data [' Year ']. ' The first $data [' Period ']. ' (' Content-disposition:attachment;filename= '. $name. '. xls '); header (' cache-control:max-age=0 ');

The meaning of this code, is to convert the Utf-8 format into a gb2312 format, and then assign to $name, so export the name of the Excel file, is the name of the Chinese $name.
Below is a detailed and extended usage of this function

Iconv ("UTF-8", "Gb2312//ignore", $data)

Ignore means ignoring errors at the time of conversion, and if there is no ignore argument, all strings after that character cannot be saved.

This function, Iconv (), is built into the PHP5. Thank you.


<?php echo $str = ' Hello, here is coffee! '; Echo '
'; echo iconv (' GB2312 ', ' UTF-8 ', $str); Transfer the encoding of the string from GB2312 to UTF-8 Echo '
'; Echo Iconv_substr ($STR, 1, 1, ' UTF-8 '); Intercept by the number of characters rather than byte Print_r (Iconv_get_encoding ()); Get the current page encoding information echo iconv_strlen ($str, ' UTF-8 '); Get the string length of the set encoding//There are also $content = Iconv ("UTF-8", "Gbk//translit", $content);?>

Iconv is not the default function for PHP, it is also the default installed module. Need to be installed to use.

If it is windows2000+php, you can modify the php.ini file, will extension=php_iconv.dll before the ";" Remove, while you copy your original PHP installation file under the Iconv.dll to your winnt/system32 (if your DLL is pointing to this directory)

In the Linux environment, the static installation of the way, in the configure when adding a--with-iconv on it, phpinfo see iconv items. (linux7.3+apache4.06+php4.3.2),


#cp Libiconv-1.8.tar.gz/usr/local/src#tar zxvf lib*#./configure--prefix=/usr/local/libiconv#make#make Install

Compiling PHP


Simple examples of use:

<?php  Echo iconv ("gb2312", "iso-8859-1", "we");?>

Introduction to Mb_convert_encoding and ICONV functions in PHP

Mb_convert_encoding This function is used to convert the encoding. It turns out that the concept of program coding is not understood, but it seems to be a bit enlightened now.
However, there is generally no coding problem in English, only the Chinese data will have this problem. For example, when you write a program with Zend Studio or editplus, using GBK encoding, if the data needs to enter the database, and the database encoding is UTF8, then the data will be encoded conversion, or into the database will become garbled.

Mb_convert_encoding's usage is in the official view:

Make a GBK to UTF-8

< PHP  header ("content-type:text/html; Charset=utf-8 ");  

One more GB2312 to Big5.

< PHP  header ("content-type:text/html; Charset=big5 ");  

However, to use the above function requires installation but first enable the mbstring extension library.

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

Here are a few more examples:

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


String mb_convert_encoding (String str, string to_encoding [, mixed from_encoding])
Need to enable Mbstring expansion Library, in php.ini; Extension=php_mbstring.dll in front of; Remove
Mb_convert_encoding can specify a variety of input encoding, it will automatically identify according to the content, but the execution efficiency is much worse than the iconv;

String Iconv (String in_charset, String out_charset, String str)
Note: The second parameter, in addition to specifying the encoding to be converted to, can also add two suffixes://translit and//ignore, where//translit automatically converts characters that cannot be converted directly into one or more approximate characters,//ignore Characters that cannot be converted are ignored, and the default effect is truncated from the first illegal character.
Returns the converted string or FALSE on failure.


The iconv is found to have an error converting the character "-" to gb2312, and if there is no ignore parameter, all strings after that character cannot be saved. In any case, this "-" can not be converted successfully, unable to output. Another mb_convert_encoding does not have this bug.

In general, with Iconv, only use the Mb_convert_encoding function if you encounter an inability to determine what encoding the original encoding is, or if the Iconv conversion fails to display properly.

From_encoding is specified by character code name before conversion. It can be array or STRING-COMMA separated enumerated list. If It is not specified, the internal encoding would be used.
/* Auto detect encoding from JIS, Eucjp-win, Sjis-win, then convert str to Ucs-2le */
$str = mb_convert_encoding ($str, "Ucs-2le", "JIS, Eucjp-win, Sjis-win");
/* "Auto" is expanded to "ascii,jis,utf-8,euc-jp,sjis" */
$str = mb_convert_encoding ($str, "EUC-JP", "Auto");


$content = Iconv ("GBK", "Utf-8″, $content);  

Parameters that are easy to ignore when using the Iconv function in PHP

Today in the processing of crawling content, when the use of ICONV encoding conversion, found that the results will be interrupted, guess is the character set problem, consider how to skip the target character set does not exist characters, check the manual found that the function of Iconv only three parameters, as if not, and then check online that some people say yes, But it is very strange how to achieve, finally found that the English description can be added to the target code behind: "Translit", very depressed how to add it? The original is to add "//", really depressed, unexpectedly have such a design
Prototype: $txtContent = Iconv ("Utf-8", ' GBK ', $txtContent);

Special parameters: Iconv ("UTF-8", "Gb2312//ignore", $data)

Two optional auxiliary parameters: Translit and ignore (where ignore means to skip if it encounters an inability to convert).

The above mentioned is the whole content of this article, I hope you can like. true techarticle php iconv Function Knowledge Summary, Phpiconv function Summary today in the revision of the paper online, encountered iconv this function. Learn the header (' Content-type:application/ ...

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