您好,欢迎访问一九零五行业门户网

完美的2个php检测字符串是否是utf-8编码函数分享,字符串utf-8_PHP教程

完美的2个php检测字符串是否是utf-8编码函数分享,字符串utf-8在php开发中有时候会用到转码函数,比如iconv(),mb_convert_encoding()函数,在用函数转码的时候或者解码的时候我们有时候需要先判断当前字符串编码类型,不如是否是utf-8编码,是的话然后进行编码转换等操作。下面是小编整理的目前web开发中网上使用率比较高的、好的php关于utf-8编码的判断函数,代码如下:
function is_utf8($string) //函数一{// from http://w3.org/international/questions/qa-forms-utf-8.htmlreturn 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′;}
mb_detect_encoding()函数是php的一个内置函数,用来判断当前字符串编码类型,此函数有三个参数,第一个参数是要判断的字符串,第二个参数是比较的字符编码列表,可以使字符串,也可以是数组,第三个参数是要求。
希望这两个函数对需要的phper有所帮助。
怎使用php判断一个字符串是不是utf-8编码
php用mbstring库的函数
$e=mb_detect_encoding($text, array('utf-8', 'gbk'));
switch($e){
case 'utf-8' : //如果是utf8编码
break;
case ‘gbk’: //如果是gbk编码
break
}
php中输出字符串的utf-8编码
比如输入“呵”,输出%e5%91%b5
这个是urlencode的...
http://www.bkjia.com/phpjc/851344.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/851344.htmltecharticle完美的2个php检测字符串是否是utf-8编码函数分享,字符串utf-8 在php开发中有时候会用到转码函数,比如iconv(),mb_convert_encoding()函数,在用函...
其它类似信息

推荐信息