What is the difference between strlen() and mb_strlen() in PHP?
strlen() returns the length of a string in bytes. mb_strlen() returns the length in characters. For English letters both are same, but for other languages they are different, because one character can use many bytes.
<?php
$english = "PHP";
$hindi = "नमस्ते";
echo strlen($english) . "<br>";
echo strlen($hindi) . "<br>";
echo mb_strlen($hindi, "UTF-8");
?>
Output:
Tutorials Class - Output Window
3
18
6
The Hindi word has 6 characters, but it takes 18 bytes in UTF-8. So use mb_strlen() whenever the text can have non-English letters, emoji or accents. It needs the mbstring extension, which is enabled in most servers.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |