Home > Article > Backend Development > Example analysis of strlen and mb_strlen usage in php
This article analyzes the usage of strlen and mb_strlen in php with examples. Share it with everyone for your reference, the details are as follows:
First look at the following code (file encoding utf8):
<?php $utf8_string = "abcd我你他她它"; var_dump(strlen($utf8_string)); var_dump(mb_strlen($utf8_string, 'gb2312')); var_dump(mb_strlen($utf8_string, 'utf8')); var_dump(mb_strlen($utf8_string)); ?>
Running result:
int 19 int 13 int 9 int 19
When the second parameter is the same as the original encoding of the string, the real number of words in the sense of human understanding will be calculated. (Chinese characters only count once)
I hope this article will be helpful to everyone in PHP programming.
For more related articles on the usage examples of strlen and mb_strlen in PHP, please pay attention to the PHP Chinese website!