PHP Character Set conversion iconv function use summary, character set conversion iconv_php tutorial

Source: Internet
Author: User
Tags php programming translit truncated

PHP Character Set conversion iconv function use summary, character set conversion iconv


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

Use the following:

Copy the Code code as follows:
$string = "Welcome to the home of the help guests!" ";
Iconv ("UTF8", "GBK", $string)//converts string encoding from UTF8 to GBK;

The extensions are as follows:

Copy the Code code as follows:
echo $str = ' Welcome to the home of the help guests! ';
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);

Note:

1, Iconv is not the default PHP function, is also the default installation of the 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, at the same time you want to copy your original PHP installation file under the Iconv.dll to your winnt/system32 (if your DLL points to this directory, my local do not operate this step)
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);

The iconv in this PHP are complete

2, using the Iconv function to crawl over the Utf-8 encoded page into gb2312, found that only with the ICONV function to crawl over the data of a transcoding data will be less for no reason. The reason is this:
Copy the 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 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. Is truncated, of course, will be less;

This can be modified Iconv ("UTF-8", "Gb2312//ignore", $string) will ignore the 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, similar to the ICONV function.

There is generally no coding problem in English, only 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:

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

Make a GBK to UTF-8

Copy the 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");
?>

One more GB2312 to Big5.

Copy the Code code as follows:
<?php
Header ("content-type:text/html; Charset=big5 ");
Echo mb_convert_encoding ("Friends", "Big5", "GB2312");
?>

However, to use the above functions need to be installed but need to enable the Mbstring extension library, the same reason is the same, modify the php.ini.
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:

Copy the 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 php.ini; Extension=php_mbstring.dll in front of; Remove
Mb_convert_encoding can specify a number of input encodings, which will be automatically identified according to the content, but the execution efficiency is much worse than the iconv, why do you want to explain this mb_convert_encoding () function? The answers are as follows:

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.


PHP uses the Iconv function to move a string from UTF8 to GBK, preserving a special string!

It won't show up. Don't use the GBK format.

iconv function Problems in PHP

Iconv (' GBK ', ' utf-8 ', $tomename);
You try to write like this, I always use it.

What does the igmore behind the Utf-8 mean? Ask

http://www.bkjia.com/PHPjc/892263.html www.bkjia.com true http://www.bkjia.com/PHPjc/892263.html techarticle PHP Character Set conversion iconv function use summary, character set conversion iconv Iconv function library can complete the conversion between various character sets, is an indispensable base Function library in PHP programming. To use as a.

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