Home >Backend Development >PHP Tutorial >PHP function example to determine whether string encoding is utf8

PHP function example to determine whether string encoding is utf8

WBOY
WBOYOriginal
2016-07-25 08:57:22923browse
  1. /**
  2. * Determine string encoding
  3. * edit by bbs.it-home.org
  4. */
  5. function is_utf8($word)
  6. {
  7. if(preg_match("/^([".chr(228)."-".chr( 233)."]developed[".chr(128)."-".chr(191)."]developed[".chr(128)."-".chr(191)."]developed)developed/" ,$word) == true || preg_match("/([".chr(228)."-".chr(233)."]developed[".chr(128)."-".chr(191) ."]developed[".chr(128)."-".chr(191)."]developed)developed$/",$word) == true || preg_match("/([".chr(228) ."-".chr(233)."]Developed[".chr(128)."-".chr(191)."]Developed[".chr(128)."-".chr(191). "]Developed){2,}/",$word) == true) {
  8. return true;
  9. }else {
  10. return false;
  11. }
  12. }
  13. $t = 'wangbin';
  14. //$t = iconv ('GB2312','UTF-8',$t)
  15. var_dump(is_utf8($t));
  16. ?>
Copy code

In addition, the function mb_detect_encoding in PHP can also implement such a function .

Detect whether the string is utf8 encoded code under PHP, function: mb_detect_encoding, this requires the mb_string library to be installed in the PHP environment. For related information about the mb_detect_encoding function, you can refer to: php function mb_detect_encoding to obtain string encoding php mb_detect_encoding detects the problem of incorrect string encoding The implemented function is as follows:

  1. /**
  2. * Check whether it is utf8 encoding
  3. * edit by bbs.it-home.org
  4. */
  5. function is_utf8($string) {
  6. return preg_match('%^(?:
  7. [x09x0Ax0Dx20-x7E] # ASCII
  8. | [xC2-xDF][x80-xBF] # non-overlong 2-byte
  9. | xE0[xA0-xBF][x80-xBF] # excluding overlongs
  10. | [xE1-xECxEExEF][x80-xBF]{2} # straight 3-byte
  11. | xED[x80-x9F][x80-xBF] # excluding surrogates
  12. | x80-xBF]{3} # planes 4-15
  13. | xF4[x80-x8F][x80-xBF]{2} # plane 16
  14. )*$%xs', $string);
  15. }
  16. ?>
Copy code
Instructions: The accuracy is basically the same as mb_detect_encoding, right or wrong. But it is basically enough for daily development. I hope everyone likes it.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn