Home > Backend Development > PHP Tutorial > php 惯用函数

php 惯用函数

WBOY
Release: 2016-06-13 13:10:52
Original
764 people have browsed it

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);
}

Related labels:
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