CakeFest 2024: The Official CakePHP Conference

grapheme_extract

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)

grapheme_extractデフォルトの書記素クラスタシーケンスをテキストバッファから取り出す関数。 テキストは UTF-8 でエンコードされている必要があります

説明

手続き型

grapheme_extract(
    string $haystack,
    int $size,
    int $type = GRAPHEME_EXTR_COUNT,
    int $offset = 0,
    int &$next = null
): string|false

デフォルトの書記素クラスタシーケンスをテキストバッファから取り出す関数です。 テキストは UTF-8 でエンコードされている必要があります。

パラメータ

haystack

検索する文字列。

size

返す項目 (type に基づく) の最大数。

type

size パラメータの単位の形式を指定します。

  • GRAPHEME_EXTR_COUNT (デフォルト) - size は、 抽出するデフォルト書記素クラスタの数です。
  • GRAPHEME_EXTR_MAXBYTES - size は、返すバイト数の最大値です。
  • GRAPHEME_EXTR_MAXCHARS - size は、返す UTF-8 文字数の最大値です。

offset

haystack 内での開始位置をバイト数で指定します。 指定する場合は、0 から haystack のバイト数までの値でなければなりません。 負の値を指定すると、haystack の末尾からバイト数を数えます。 offset が指す位置が UTF-8 文字の先頭バイトでない場合は、 その次の文字の先頭から開始します。

next

次の開始位置への参照が設定されます。 この関数をコールすると、文字列の最後の部分の先頭バイトを指すことになります。

戻り値

オフセット offset から始まり、 指定した sizetype を満たす、 デフォルトの書記素クラスタバウンダリで終わる文字列を返します。 失敗した場合に false を返します

変更履歴

バージョン 説明
7.1.0 負の offset をサポートするようになりました。

例1 grapheme_extract() の例

<?php

$char_a_ring_nfd
= "a\xCC\x8A"; // 'LATIN SMALL LETTER A WITH RING ABOVE' (U+00E5) normalization form "D"
$char_o_diaeresis_nfd = "o\xCC\x88"; // 'LATIN SMALL LETTER O WITH DIAERESIS' (U+00F6) normalization form "D"

print urlencode(grapheme_extract( $char_a_ring_nfd . $char_o_diaeresis_nfd, 1, GRAPHEME_EXTR_COUNT, 2));

?>

上の例の出力は以下となります。

o%CC%88
add a note

User Contributed Notes 3 notes

up
5
AJH
12 years ago
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!
?>
up
1
Philo
4 months ago
The other comments on this page were helpful for me.
However, consider using something better than empty($value) when checking the value returned by grapheme_extract since it could as well return something like "0" (which of course evaluates to false).
up
1
yevgen dot grytsay at gmail dot com
3 years ago
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
?>
To Top