What is the difference between the iconv_strlen, mb_strlen and strlen functions?
And what is each used for in the real world?
Function strlen() is used when we require the string length showing a number of bytes whereas function mb_strlen() is used when we require the string length showing number of characters. The mb in the mb_strlen() function stands for multibyte.
In PHP, multibyte string length (mb_strlen) function is used to get the total string length of a specified string. This function is supported in PHP 4.6. 0 or higher versions.
The strlen() function returns the length of a string.
The strlen() is a built-in function in PHP which returns the length of a given string. It takes a string as a parameter and returns its length. It calculates the length of the string including all the whitespaces and special characters.
The difference of strlen()
to mb_strlen()
is, that the second respects multibyte characters. This means, that this is the real character count. The first one assumes, that the string is always in ascii, what also means, that it always returns the size in bytes (very useful when handling binary "strings").
As far as I can see iconv_strlen()
is quite similar to mb_strlen()
, but fails on bad character sequences, in contrast to mb_strlen()
, that just ignores them. And it uses the iconv
-library (obviously ;)).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With