Php string encoding and conversion program

Source: Internet
Author: User
A bug in the iconv function. Iconv will encounter an error when converting characters & mdash; to gb2312. the solution is very simple: add IGNORE (the second parameter of the iconv function) after the code to be converted

A bug in the iconv function. Iconv will encounter an error when converting the characters "-" to gb2312. the solution is very simple: add "// IGNORE" after the encoding to be converted, that is, after the second parameter of the iconv function. reference content is as follows:

Iconv ("UTF-8", "GB2312 // IGNORE", $ data)

Ignore indicates that the conversion error is ignored. without the ignore parameter, all strings after this character cannot be saved. this iconv () function is built-in in php5, the instance code is as follows:

  1. Echo $ str = 'Hi, it's coffee sale! ';
  2. Echo' ';
  3. Echo iconv ('gb2312', 'utf-8', $ str); // Encode the string from GB2312 to UTF-8
  4. Echo' ';
  5. Echo iconv_substr ($ str, 1, 1, 'utf-8'); // truncate by number of characters rather than bytes
  6. Print_r (iconv_get_encoding (); // Obtain the encoding information of the current page.
  7. Echo iconv_strlen ($ str, 'utf-8'); // you can specify the length of the encoded string.
  8. // This is also applicable.
  9. $ Content = iconv ("UTF-8", "gbk // transcoder", $ content );
  10. ?>

However, using the iconv function may encounter a problem such as notice: iconv () [function. iconv]: detected an illegal character in input string... the error occurs because the encoding range is incorrect. if gb2312 is smaller than gbk and is smaller than uft8, you must pay attention to this during conversion, however, php also provides a function mb_detect_encoding, which can better solve this problem. now we can write it into a more professional function.

  1. Function phpcharset ($ data, $ ){
  2. If (is_array ($ data )){
  3. Foreach ($ data as $ key => $ val ){
  4. $ Data [$ key] = phpcharset ($ val, $ );
  5. }
  6. } Else {
  7. $ Encode_array = array ('ascii ', 'utf-8', 'gbk', 'gb2312', 'big5 ');
  8. $ Encoded = mb_detect_encoding ($ data, $ encode_array );
  9. $ To = strtoupper ($ );
  10. If ($ encoded! = $ ){
  11. $ Data = mb_convert_encoding ($ data, $ to, $ encoded );
  12. }
  13. }
  14. Return $ data;
  15. }
  16. ?>

Sometimes we do not know the character encoding. in this case, we need to detect the encoding before conversion. the code is as follows:

  1. Function asciitog ($ brand)
  2. {
  3. $ Cha = mb_detect_encoding ($ brand );
  4. If ($ cha = 'utf-8 ')
  5. {
  6. $ Brand2 = iconv ($ cha, "gb2312", $ brand );
  7. }
  8. $ Cha2 = mb_detect_encoding ($ brand2 );
  9. If ($ cha2! = 'Ascii '){
  10. $ Brand = $ brand2;
  11. }
  12.  
  13. Return $ brand;
  14. }

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.