note 104872 added to function.grapheme-extract
| From: | AJH at osu1 dot php dot net | Date: | Tue, 12 Jul 2011 23:37:58 +0000 |
| Subject: | note 104872 added to function.grapheme-extract | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181448@lists.php.net to get a copy of this message | ||
Here's how to use grapheme_extract() to loop across a UTF-8 string character by character.
<?php
$str = "سabcà¤ââ¦";
// if the previous line didn't come through, the string contained:
//U+0633,U+0061,U+0062,U+0063,U+0915,U+2019,U+2026
$n = 0;
for ( $start = 0, $next = 0, $maxbytes = strlen($str), $c = '';
$start < $maxbytes;
$c = grapheme_extract($str, 1, GRAPHEME_EXTR_MAXCHARS , ($start = $next), $next)
)
{
if (empty($c))
continue;
echo "This utf8 character is " . strlen($c) . " bytes long and its first byte is
" . ord($c[0]) . "\n";
$n++;
}
echo "$n UTF-8 characters in a string of $maxbytes bytes!\n";
// Should print: 7 UTF8 characters in a string of 14 bytes!
?>
----
Server IP: 87.124.126.116
Probable Submitter: 92.234.209.141 (proxied: 92.234.209.141)
----
Manual Page -- http://www.php.net/manual/en/function.grapheme-extract.php
Edit -- https://master.php.net/note/edit/104872
Del: integrated -- https://master.php.net/note/delete/104872/integrated
Del: useless -- https://master.php.net/note/delete/104872/useless
Del: bad code -- https://master.php.net/note/delete/104872/bad+code
Del: spam -- https://master.php.net/note/delete/104872/spam
Del: non-english -- https://master.php.net/note/delete/104872/non-english
Del: in docs -- https://master.php.net/note/delete/104872/in+docs
Del: other reasons-- https://master.php.net/note/delete/104872
Reject -- https://master.php.net/note/reject/104872
Search -- https://master.php.net/manage/user-notes.php