PHP Judgment string encoding function _php Tutorial

Source: Internet
Author: User
Character encoding judgment is something we use all the time, especially when I want to judge what the user has entered or submitted by the character encoding to be effectively processed, let me introduce you to PHP judgment string encoding function

Mb_detect_encoding () ($STR);

The code is as follows Copy Code


Determine what encoding a string is
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 ');
}

The function can detect the encoding but using this function must open PHP's Extension=php_mbstring.dll extension if everyone is using space and not modified

PHP.ini Configuration folder permissions will there be better functions to check the string encoding, promise is yes

Determine if a string is UTF-8 encoded

The code is as follows Copy Code

/**
+----------------------------------------------------------
* Check if 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?

copy code

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

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

Encoding
return false;
Else
Retur n true;
}
}
}
return true;
}

Some friends say you can use the Mb_check_encoding function to check, this I have not tested everyone can test it yourself oh.

http://www.bkjia.com/PHPjc/630722.html www.bkjia.com true http://www.bkjia.com/PHPjc/630722.html techarticle character encoding judgment is something that we use all the time, especially when I want to judge what the user has entered or the character that is being submitted is encoded so that it can be effectively processed, let me give the big ...

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