PHP Judgment string encoding function

Source: Internet
Author: User

Mb_detect_encoding () ($STR);

The code is as follows Copy Code


Determine what the string is encoded
if ($tag = = mb_convert_encoding (mb_convert_encoding ($tag, "GB2312", "UTF-8"), "UTF-8", "GB2312")) {

}
else {//if the gb2312 is converted to UTF8
$tag = mb_convert_encoding ($tag, ' UTF-8 ', ' GB2312 ');
}

function can detect encoding but using this function you must open the Extension=php_mbstring.dll extension of PHP, if you are using space without modification

PHP.ini the permissions of the configuration folder will there be a better function to check the string encoding? Yes, yes.

To determine whether a string is UTF-8 encoded

The code is as follows Copy Code

/**
+----------------------------------------------------------
* Check whether the string is UTF8 encoded
+----------------------------------------------------------
* @param string $string strings
+----------------------------------------------------------
* @return Boolean
+----------------------------------------------------------
*/
function Is_utf8 ($string)
{
Return Preg_match ('%^:
[x09x0ax0dx20-x7e] # ASCII
| [XC2-XDF] [X80-XBF] # Non-overlong 2-byte
| XE0[XA0-XBF][X80-XBF] # excluding overlongs
| [Xe1-xecxeexef] [X80-XBF] {2} # straight 3-byte
| XED[X80-X9F][X80-XBF] # excluding surrogates
| XF0[X90-XBF][X80-XBF]{2} # Planes 1-3
| [Xf1-xf3] [X80-XBF] {3} # planes 4-15
| XF4[X80-X8F][X80-XBF]{2} # Plane 16
) *$%xs ', $string);
}

Can you check out GB2312 or UTF-8?

The code is as follows Copy Code

Function is_gb2312 ($str)
{
        for ($i =0; $i <strlen ($STR); $i + +) {
                $v = Ord ($str [$i] );
                if ($v > 127) {
                         if (($v >= 228) && ($v <= 233))
     & nbsp;                  {
                                 if (($i +2) >= (strlen ($str )-1) true; //Not enough

Characters
$v 1 = ord ($str [$i +1]);
$v 2 = Ord ($str [$i +2]);
if ($v 1 >= 128) && ($v 1 <=191) && ($v 2 >=128) && ($v 2 <= 191))//UTF

Coding
return false;
Else
return true;
}
}
}
return true;
}

Some friends say you can use the Mb_check_encoding function to check, this I did not test everyone can test OH.

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.