utf8字符串长度 [英] utf8 string length

查看:156
本文介绍了utf8字符串长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

strlen()函数无法正确返回utf8字符的字符串长度,例如سلام为4字符,但是在使用strlen thats后返回8字符

strlen() function in php could not return correctly string lenght of utf8 chars, for example سلام is 4 char but after using strlen thats return 8 chr

<?php
echo strlen('سلام');
?>

推荐答案

PHP核心字符串函数均假定1个字符= 1个字节.它们没有不同编码的概念.要确定UTF-8字符串中有多少个字符(而不是多少个 bytes ),请使用

The core PHP string functions all assume 1 character = 1 byte. They have no concept of different encodings. To figure out how many characters are in a UTF-8 string (not how many bytes), use the mb_strlen equivalent and tell it what encoding the string is in:

echo mb_strlen('سلام', 'UTF-8');

这篇关于utf8字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆