문제

There are multibyte string functions in PHP to handle multibyte string (e.g:CJK script). For example, I want to count how many letters in a multi bytes string by using len function in python, but it return an inaccurate result (i.e number of bytes in this string)

japanese = "桜の花びらたち"
print japanese
print len(japanese)#return 21 instead of 7

Is there any package or function like mb_strlen in PHP?

도움이 되었습니까?

해결책

Use Unicode strings:

# Encoding: UTF-8

japanese = u"桜の花びらたち"
print japanese
print len(japanese)

Note the u in front of the string.

To convert a bytestring into Unicode, use decode: "桜の花びらたち".decode('utf-8')

다른 팁

Try converting it to unicode first:

print len(japanese.decode("utf-8"))

gives 7. You are working on the utf-8 encoded string, which indeed has 21 bytes.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top