Common methods for PHP to detect whether a string is UTF-8 encoded

Source: Internet
Author: User
This article mainly introduces the common methods for PHP to check whether the string is UTF-8 encoded. it lists four examples to implement this function from different perspectives. it is a very practical technique, it has some learning reference value. if you need it, you can refer to the examples in this article to summarize the common methods for PHP to detect whether the string is UTF-8 encoded. Share it with you for your reference. The specific implementation method is as follows:

There are many methods to detect String Encoding. for example, you can use ord to obtain the character base and then enter the judgment, or use the mb_detect_encoding function for processing. The following describes four common methods for your reference.

Example 1

The code is as follows:

/**
* Checks whether the string is UTF-8 encoded.
* @ Param string $ str the string to be detected
* @ Return boolean
*/
Function is_utf8 ($ str ){
$ Len = strlen ($ str );
For ($ I = 0; $ I <$ len; $ I ++ ){
$ C = ord ($ str [$ I]);
If ($ c & gt; 128 ){
If ($ c> 247) return false;
Elseif ($ c & gt; 239) $ bytes = 4;
Elseif ($ c> 223) $ bytes = 3;
Elseif ($ c> 191) $ bytes = 2;
Else return false;
If ($ I + $ bytes)> $ len) return false;
While ($ bytes> 1 ){
$ I ++;
$ B = ord ($ str [$ I]);
If ($ B <128 | $ B> 191) return false;
$ Bytes --;
}
}
}
Return true;
}


Example 2

The code is as follows:

Function is_utf8 ($ string ){
Return preg_match ('% ^ (? :
[\ X09 \ x0A \ x0D \ x20-\ x7E] # ASCII
| [\ XC2-\ xDF] [\ x80-\ xBF] # non-overlong 2-byte
| \ XE0 [\ xA0-\ xBF] [\ x80-\ xBF] # excluding overlongs
| [\ XE1-\ xEC \ xEE \ xEF] [\ 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 );
}


The accuracy is basically the same as that of mb_detect_encoding (). If the accuracy is correct, an error is returned.
Encoding detection cannot be 100% accurate, which can basically meet requirements.
Example 3

The code is as follows:

Function mb_is_utf8 ($ string)
{
Return mb_detect_encoding ($ string, 'utf-8') === 'utf-8'; // Newly discovered
}

Example 4

The code is as follows:

// Returns true if $ string is valid UTF-8 and false otherwise.
Function is_utf8 ($ word)
{
If (preg_match ("/^ ([". chr (1, 228 ). "-". chr (1, 233 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1}) {1}/", $ word) = true | preg_match ("/([". chr (1, 228 ). "-". chr (1, 233 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1}) {1} $/", $ word) = true | preg_match ("/([". chr (1, 228 ). "-". chr (1, 233 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1}) {2,}/", $ word) = true)
{
Return true;
}
Else
{
Return false;
}
} // Function is_utf8

I hope this article will help you with PHP programming.

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.