note 91033 added to function.mb-strcut

From: Date: Thu, 21 May 2009 16:07:21 +0000
Subject: note 91033 added to function.mb-strcut
Groups: php.notes 
Request: Send a blank email to php-notes+get-155853@lists.php.net to get a copy of this message
I found this function to be extremely useful. Here is a practical example, showing the difference between substr(), mb_substr() and mb_strcut(): <?php mb_internal_encoding('UTF-8'); $string = 'cioèòà'; var_dump( substr($string, 0, 6), mb_substr($string, 0, 6), mb_strcut($string, 0, 6) ); ?> Output: string(6) "cioè?" string(9) "cioèòà" string(5) "cioè" Explanation: $string is long 9 bytes c - 1 byte i - 1 byte o - 1 byte è - 2 bytes ò - 2 bytes à - 2 bytes substr() works with bytes, so it returns a string which is exactly 6 bytes long. Thus, it truncates the ò character. mb_substr(), instead, works with characters, so it returns a string which is exactly 6 characters long (but in this case is 9 bytes long). mb_strcut() works exactly as substr(), but, if the last byte appears to be truncated, it simply omits the character. When you use $string = mb_strcut($string, 6); you can know for sure that strlen($string) <= 6. But no unicode characters will be truncated. I hope my comment could finally be a simple explanation. ---- Server IP: 213.92.11.19 Probable Submitter: 79.44.252.254 ---- Manual Page -- http://www.php.net/manual/en/function.mb-strcut.php Edit -- https://master.php.net/note/edit/91033 Del: integrated -- https://master.php.net/note/delete/91033/integrated Del: useless -- https://master.php.net/note/delete/91033/useless Del: bad code -- https://master.php.net/note/delete/91033/bad+code Del: spam -- https://master.php.net/note/delete/91033/spam Del: non-english -- https://master.php.net/note/delete/91033/non-english Del: in docs -- https://master.php.net/note/delete/91033/in+docs Del: other reasons-- https://master.php.net/note/delete/91033 Reject -- https://master.php.net/note/reject/91033 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#155853) next »