note 118484 added to function.strlen
| From: | rm dot nasir at hotmail dot com | Date: | Mon, 14 Dec 2015 09:08:36 +0000 |
| Subject: | note 118484 added to function.strlen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204344@lists.php.net to get a copy of this message | ||
I want to share something seriously important for newbies or beginners of PHP who plays with strings
of UTF8 encoded characters or the languages like: Arabic, Persian, Pashto, Dari, Chinese
(simplified), Chinese (traditional), Japanese, Vietnamese, Urdu, Macedonian, Lithuanian, and etc.
As the manual says: "strlen() returns the number of bytes rather than the number of characters
in a string.", so if you want to get the number of characters in a string of UTF8 so use
mb_strlen() instead of strlen().
Example:
<?php
// the Arabic (Hello) string below is: 59 bytes and 32 characters
$utf8 = "Ø§ÙØ³ÙاÙ
عÙÛÚ©Ù
ÙØ±ØÙ
Ø© اÙÙÙ
ÙØ¨Ø±Ú©Ø§ØªÙ!";
var_export( strlen($utf8) ); // 59
echo "<br>";
var_export( mb_strlen($utf8, 'utf8') ); // 32
?>
----
Server IP: 72.52.91.14
Probable Submitter: 175.106.44.235
----
Manual Page -- http://php.net/manual/en/function.strlen.php
Edit -- https://master.php.net/note/edit/118484
Del: integrated -- https://master.php.net/note/delete/118484/integrated
Del: useless -- https://master.php.net/note/delete/118484/useless
Del: bad code -- https://master.php.net/note/delete/118484/bad+code
Del: spam -- https://master.php.net/note/delete/118484/spam
Del: non-english -- https://master.php.net/note/delete/118484/non-english
Del: in docs -- https://master.php.net/note/delete/118484/in+docs
Del: other reasons-- https://master.php.net/note/delete/118484
Reject -- https://master.php.net/note/reject/118484
Search -- https://master.php.net/manage/user-notes.php