PHP iconv function usage examples and precautions

WBOY
Release: 2016-07-25 08:46:37
Original
1105 people have browsed it
  1. echo $str= 'Hello, we sell coffee here!';
  2. echo '
    ';
  3. echo iconv('GB2312', 'UTF-8', $str); //Convert the string encoding from GB2312 to UTF-8
  4. echo '
    ';
  5. echo iconv_substr($str, 1, 1, 'UTF-8'); //Press Truncate the number of characters instead of bytes
  6. print_r(iconv_get_encoding()); //Get the encoding information of the current page
  7. echo iconv_strlen($str, 'UTF-8'); //Get the string length of the set encoding
  8. // There is also bbs.it-home.org that is used like this
  9. $content = iconv("UTF-8","gbk//TRANSLIT",$content);
  10. ?>
Copy code

Notes on using iconv function in php

When using iconv() to convert a character that is not supported by the output character encoding, such as iconv('UTF-8', 'GB2312', '囧'), you will encounter this error message: Notice: iconv() [function.iconv]: Detected an illegal character in input string ... Because GB2312 represents Simplified Chinese and does not support more complex Chinese characters such as "囧" and some special characters, this will of course result in an error.

There are two solutions: 1. Expand the range of output character encoding, such as iconv('UTF-8', 'GBK', '囧'), which can be output correctly because GBK supports a wider range of characters; 2. Add "//IGNORE" after the output character encoding string, such as iconv('UTF-8', 'GB2312//IGNORE', '囧'), which ignores characters that cannot be converted and avoids errors. But it cannot be output correctly (that is, blank is not output).



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!