Detailed description of iconv function usage in php

Source: Internet
Author: User

This article will introduce in detail how to use the iconv function in php to convert the string encoding method and analyze the iconv Function bug. If you need it, you may refer to it.

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

The usage is as follows:

The Code is as follows: Copy code

$ 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:

The Code is as follows: Copy code

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 );

A small bug in the iconv Function

Try converting the following string with iconv to see the effect.

The Code is as follows: Copy code

$ Str = "www.bkjia.com, OK! The string is €. I want to convert it! ⊙ ●○ ① Then ◎ then ⊙ then? ";
Echo 'has no parameters:', iconv ("UTF-8", "gbk", $ str )."
";

The results returned on my computer are not displayed after the first special character.

Iconv has two parameters: Transcoder and IGNORE, respectively. Transcoder indicates that if the original encoding cannot be translated in the target encoding, find a similar character or string replacement, for example, € may be replaced with EUR. IGNORE directly skips and ignores the original characters that cannot be translated by the target character set, false is not returned.

The usage is as follows:

The Code is as follows: Copy code

$ Infocontent = iconv ("UTF-8", "gbk // IGNORE", $ infocontent );

Or

$ Infocontent = iconv ("UTF-8", "gbk // Transcoder", $ infocontent );

In view of this, I think iconv should provide a default parameter IGNORE to avoid returning null strings when developers are not using it.


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.