PHP 中有多字节字符串函数来处理多字节字符串(例如:CJK 脚本)。例如,我想通过在 python 中使用 len
函数来计算多字节字符串中的字母数,但它返回的结果不准确(即该字符串中的字节数)
japanese = "桜の花びらたち"
print japanese
print len(japanese)#return 21 instead of 7
PHP有没有类似mb_strlen的包或者函数?
最佳答案
使用Unicode strings :
# Encoding: UTF-8
japanese = u"桜の花びらたち"
print japanese
print len(japanese)
注意字符串前面的u
。
要将字节字符串转换为 Unicode,请使用 decode
:"桜の花びらたち".decode('utf-8')
关于python - 如何在 Python 中处理多字节字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8346608/