note 125444 added to function.grapheme-extract
| From: | yevgendotgrytsayatgmaildotcom@php.net | Date: | Mon, 19 Oct 2020 13:35:01 +0000 |
| Subject: | note 125444 added to function.grapheme-extract | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-215615@lists.php.net to get a copy of this message | ||
Looping through grapheme clusters:
<?php
// Example taken from Rust documentation: https://doc.rust-lang.org/book/ch08-02-strings.html#bytes-and-scalar-values-and-grapheme-clusters-oh-my
$str = "नमसà¥à¤¤à¥";
// Alternatively:
//$str = pack('C*', ...[224, 164, 168, 224, 164, 174, 224, 164, 184, 224, 165, 141, 224,
164, 164, 224, 165, 135]);
$next = 0;
$maxbytes = strlen($str);
var_dump($str);
while ($next < $maxbytes) {
$char = grapheme_extract($str, 1, GRAPHEME_EXTR_COUNT, $next, $next);
if (empty($char)) {
continue;
}
echo "{$char} - This utf8 character is " . strlen($char) . ' bytes long',
PHP_EOL;
}
//string(18) "नमसà¥à¤¤à¥"
//न - This utf8 character is 3 bytes long
//म - This utf8 character is 3 bytes long
//सॠ- This utf8 character is 6 bytes long
//तॠ- This utf8 character is 6 bytes long
?>
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.174 (proxied: 188.163.59.133)
----
Manual Page -- http://php.net/manual/en/function.grapheme-extract.php
Edit -- https://master.php.net/note/edit/125444
Del: integrated -- https://master.php.net/note/delete/125444/integrated
Del: useless -- https://master.php.net/note/delete/125444/useless
Del: bad code -- https://master.php.net/note/delete/125444/bad+code
Del: spam -- https://master.php.net/note/delete/125444/spam
Del: non-english -- https://master.php.net/note/delete/125444/non-english
Del: in docs -- https://master.php.net/note/delete/125444/in+docs
Del: other reasons-- https://master.php.net/note/delete/125444
Reject -- https://master.php.net/note/reject/125444
Search -- https://master.php.net/manage/user-notes.php