Home  >  Article  >  Backend Development  >  php 惯用函数

php 惯用函数

WBOY
WBOYOriginal
2016-06-13 13:10:52747browse

php 常用函数

当用户提交数据为中英数字混排时计算长度

?

/**
?* UTF-8编码情况下 *
?* 计算字符串的长度 *
?* @param?? string????? $str??????? 字符串
?*
?* @return? array
?*/
function strLength($str)
{
??? if(empty($str)) return 0;
??? $length = strlen(preg_replace('/[\x00-\x7F]/', '', $str));

??? $arr['en'] = strlen($str) - $length;
??? $arr['cn'] = intval($length / 3);//编码GBK,除以2
???
???
???
??? return (int)($arr['cn'] + $arr['en']/2);
}

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