Home > Backend Development > PHP Tutorial > Perfect 2 PHP detection string is utf-8 encoding function sharing, string utf-8_PHP tutorial

Perfect 2 PHP detection string is utf-8 encoding function sharing, string utf-8_PHP tutorial

WBOY
Release: 2016-07-13 10:22:08
Original
770 people have browsed it

Perfect 2 PHP detection string is utf-8 encoding function sharing, string utf-8

In PHP development, transcoding functions are sometimes used, such as iconv() and mb_convert_encoding() functions. When using functions to transcode or decode, we sometimes need to first determine the current string encoding type. How about Whether it is UTF-8 encoding, if so, then perform encoding conversion and other operations. The following is a good PHP judgment function about UTF-8 encoding that is currently used in web development and has a relatively high online usage rate. The code is as follows:

function is_utf8($string) //函数一
{
// From http://w3.org/International/questions/qa-forms-utf-8.html
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);
}

function mb_is_utf8($string) //函数二
{
return mb_detect_encoding($string, ‘UTF-8′) === ‘UTF-8′;
}
Copy after login

The mb_detect_encoding() function is a built-in function of PHP, used to determine the current string encoding type. This function has three parameters. The first parameter is the string to be determined, and the second parameter is the character encoding list for comparison. , it can be a string or an array, and the third parameter is the requirement.
I hope these two functions will be helpful to those who need Phper.

How to use PHP to determine whether a string is UTF-8 encoded

PHP uses the function of MBString library
$e=mb_detect_encoding($text, array('UTF-8', 'GBK'));
switch($e){
case 'UTF- 8': //If it is utf8 encoding
break;
case 'GBK': //If it is gbk encoding
break
}

UTF-8 encoding of output string in php

For example, if you input "ha", the output will be %E5%91%B5
This is urlencode...

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/851344.htmlTechArticlePerfect 2 php detection string is utf-8 encoding function sharing, string utf-8 in php Transcoding functions are sometimes used in development, such as iconv(), mb_convert_encoding() function. When using the function...
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template