PHP iconv (): detected a illegal character in input string_php tips

Source: Internet
Author: User
Tags translit
That's how it started.
$str = Iconv (' UTF-8 ', ' GB2312 ', unescape (isset ($_get[' str ')) "$_get[' str ']:");
After the launch of a heap of such a mistake: Iconv (): detected an illegal character in input string

Considering the GB2312 character set is relatively small, change a large bar, and then changed to GBK:
$str = Iconv (' UTF-8 ', ' GBK ', unescape (isset ($_get[' str ')) "$_get[' str ']:");
After the on-line still report the same mistake!

Read the manual carefully and find that there is a paragraph:
If you are append the string//translit to Out_charset transliteration are activated. This means so when a character can ' t is represented in the target charset, it can be approximated through one or several Similarly looking characters. If you append the string//ignore, characters that cannot is represented in the target charset are silently.
It is then changed to:
$str = Iconv (' UTF-8 ', ' Gbk//ignore ', unescape (isset ($_get[' str ')) "$_get[' str ']:");
Local test//ignore can ignore the words it does not know and then go down, and do not complain, and//translit is to intercept the words it does not know and its subsequent content, and error. Ignore is what I need.
Now wait for the line to see the results (this is not good practice, continue to think about the handbook, the Internet search), hehe ...

Find the following article online, found that mb_convert_encoding can also, but the efficiency is worse than iconv.


conversion string encoding Iconv and mb_convert_encoding differences

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 first enable Mbstring expansion library, in the php.ini; Extension=php_mbstring.dll in front of; Remove

String Iconv (String in_charset, String out_charset, String str)
Attention:
The second parameter, in addition to specifying the encoding to convert to, can add two suffixes://translit and//ignore,
which
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.
Returns the converted string or FALSE on failure.

Use:
1. Found that iconv in the conversion character "-" to gb2312 error, if there is no ignore parameters, all the strings after the string can 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.
2. Mb_convert_encoding can specify a variety of input codes, which will be automatically identified according to the content, but the execution efficiency is much worse than iconv, such as: $str = mb_convert_encoding ($str, "EUC-JP", "Ascii,jis , euc-jp,sjis,utf-8 ");" Ascii,jis,euc-jp,sjis,utf-8 "The order of the different effects also have differences
3. In general, use the Iconv only if you encounter an inability to determine the encoding of the original encoding, or iconv the conversion is not normal to display the mb_convert_encoding function

From_encoding is specified by character code name before conversion. It can be an array or STRING-COMMA separated enumerated list. If It is not specified, the internal encoding would be used.

$str = mb_convert_encoding ($str, "Ucs-2le", "JIS, Eucjp-win, Sjis-win");
$str = mb_convert_encoding ($str, "EUC-JP", "Auto");

Example:
$content = Iconv ("GBK", "UTF-8", $content);
$content = mb_convert_encoding ($content, "UTF-8", "GBK");

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.