by SXX · 2y ago · 0 comments
TLDR: in 2024 with PHP8 you still need mbstring extension and also you should be careful around UTF-8 if you do any text processing. In almost all other modern programming languages it's just works.
The whole concept of having special Unicode-aware strlen or substr is nonsense.
> '蛋糕'.substr(0,1)
'蛋'
> '蛋糕'.length
2
> Buffer.byteLength('蛋糕')
6
You do have to be careful when working with binary data (e.g. streams) but this is expected. > '𐐩'.substr(0, 1)
'\ud801'
> '𐐩'.length
2
[1] https://en.wikipedia.org/wiki/UTF-16#DescriptionIt just so happens that your example consists of two UTF-16 codepoints.
(Node.js' Buffer uses UTF-8 by default).