最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php mb_strlen()中英混体字符截取代码
时间:2022-06-24 23:56:23 编辑:袖梨 来源:一聚教程网
注:如果在用mb_strlen出现fatal error: call to undefined function mb_strlen,这种问题你要可以用php教程info()看一下有没有装载mbstring,如果没有,尝试将php_mbstring.dll复制到%windows%目录下。
文件编码 utf-8
$var = '中文字符abc';
mb_strlen($var, 'utf-8'); // 输出7 中文英文都占一个字节
mb_strlen($var); // 输出15 中文占3个字节 英文占一个字节
mb_strlen($var, 'gbk'); // 输出9 不正常
取全部中文
function utf8substr($str, $from, $len)
{
return preg_replace('#^(?:[x00-x7f]|[xc0-xff][x80-xbf]+){0,'.$from.'}'.
'((?:[x00-x7f]|[xc0-xff][x80-xbf]+){0,'.$len.'}).*#s',
'$1',$str);
}
中文与英混体截取代码
function gb2312_strlen($string)
{
$str_len = strlen($string);
$str_count = 0;
for($j = 0; $j < $str_len; $j++)
{
if(ord($string{$j}) < 127)
{
$str_count += 1;
continue;
}
else
{
if(ord($string{$j+1}) > 127)
{
$str_count += 1;
$j++;
continue;
}
else
{
$str_count += 1;
continue;
}
}
}
return $str_count;
}
$str = "开s d";
echo gb2312_strlen($str);
相关文章
- 整理拾光全部隐藏物品获取方法攻略 12-04
- 魔理沙还书姬虫百百世彩蛋位置一览 12-04
- 阿西美女室友竟然2第六章全流程攻略 12-04
- 超英派遣中心第四章第一轮班派遣攻略 12-04
- 阿西美女室友竟然2第五章全流程攻略 12-04
- 梦之形跳跳锤维斯珀玩法攻略分享 12-04