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.