Questions about Iconv and mb_detetct_encoding in mb_convert_encoding-php

Source: Internet
Author: User

Update 6.11 After looking more closely at the comments on mb_detect_encoding in the PHP manual,

If you try to use mb_detect_encoding to detect whether a string is valid UTF-8, use the strict mode, it is pretty worthless otherwise.
  
   

I have another question, in the detection of GBK mb_detect_encoding return is cp936
I need to judge this value and then use Iconv (' GBK ', ' utf-8 ', $str) to convert, if it's something else.

GBK corresponds to the corresponding cp936, how should I match in the detection method? "


$a=' "name":"默认排序", "key":"sort", "value":"", "status":"0", "isSelected": "0" }';// var_dump($b);$b=iconv('UTF-8', 'GBK', $a);$x=mb_detect_encoding($a);$y=mb_detect_encoding($b);var_dump($x,$y);var_dump($b);


The sample is in the browser code UTF8, after the code changed to GBK Chinese normal display

Question: 1, why the Iconv converted string is still utf-8
2, mb_detect_encoding exactly is what code to detect
3, the production of such a problem, I can still use mb_convert_encoding transcoding?

Reply content:

Update 6.11 After looking more closely at the comments on mb_detect_encoding in the PHP manual,

If you try to use mb_detect_encoding to detect whether a string is valid UTF-8, use the strict mode, it is pretty worthless otherwise.
  
   

I have another question, in the detection of GBK mb_detect_encoding return is cp936
I need to judge this value and then use Iconv (' GBK ', ' utf-8 ', $str) to convert, if it's something else.

GBK corresponds to the corresponding cp936, how should I match in the detection method? "


$a=' "name":"默认排序", "key":"sort", "value":"", "status":"0", "isSelected": "0" }';// var_dump($b);$b=iconv('UTF-8', 'GBK', $a);$x=mb_detect_encoding($a);$y=mb_detect_encoding($b);var_dump($x,$y);var_dump($b);


The sample is in the browser code UTF8, after the code changed to GBK Chinese normal display

Question: 1, why the Iconv converted string is still utf-8
2, mb_detect_encoding exactly is what code to detect
3, the production of such a problem, I can still use mb_convert_encoding transcoding?

First, look at this.
http://cn2.php.net/manual/zh/function.mb-detect-encoding.php
http://cn2.php.net/manual/zh/function.mb-detect-order.php

Ps.
If you want to output JSON Chinese, consider using the Servicesjson class.

The solution is: change Utf-8 to UTF8

  • 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.