Home  >  Article  >  Backend Development  >  PHP mb_convert_encoding 获取字符串编码类型实现代码_php技巧

PHP mb_convert_encoding 获取字符串编码类型实现代码_php技巧

WBOY
WBOYOriginal
2016-05-17 09:32:42828browse

后来又在手册上找到了is_utf8函数,这样,再结合iconv函数,我的问题就解决了。下面帖出这个函数:

复制代码 代码如下:

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);
} // function is_utf8


如果想深入研究,建议看下PHP手册上的“Multibyte String Functions”这一部分的内容。
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