-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PHP 8.4][Intl] Add
grapheme_str_split
Add a polyfill for the `grapheme_str_split` function added in PHP 8.4. Requires PHP 7.3, because the polyfill is based on `\X` Regex, and it only works properly on PCRE2, which [only comes with PHP 7.3+](https://php.watch/versions/7.3/pcre2). Further, there are some cases that the polyfill cannot split complex characters (such as two consecutive country flag Emojis). This is now fixed in [PCRE2Project/pcre2#410](PCRE2Project/pcre2#410). However, this change will likely only make it to PHP 8.4. References: - [RFC: Grapheme cluster for `str_split` function: `grapheme_str_split`](https://wiki.php.net/rfc/grapheme_str_split) - [PHP.Watch: PHP 8.4: New `grapheme_str_split` function](https://php.watch/versions/8.4/grapheme_str_split)
- Loading branch information
Showing
9 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
* - grapheme_strrpos - Find position (in grapheme units) of last occurrence of a string | ||
* - grapheme_strstr - Returns part of haystack string from the first occurrence of needle to the end of haystack | ||
* - grapheme_substr - Return part of a string | ||
* - grapheme_str_split - Splits a string into an array of individual or chunks of graphemes. | ||
* | ||
* @author Nicolas Grekas <[email protected]> | ||
* | ||
|
@@ -191,6 +192,36 @@ public static function grapheme_strstr($s, $needle, $beforeNeedle = false) | |
return mb_strstr($s, $needle, $beforeNeedle, 'UTF-8'); | ||
} | ||
|
||
public static function grapheme_str_split($s, $len = 1) { | ||
if (0 > $len || 1073741823 < $len) { | ||
if (80000 > \PHP_VERSION_ID) { | ||
return false; | ||
} | ||
|
||
throw new \ValueError('grapheme_str_split(): Argument #2 ($length) must be greater than 0 and less than or equal to 1073741823.'); | ||
} | ||
|
||
if ('' === $s) { | ||
return []; | ||
} | ||
|
||
if (!preg_match_all('/('.SYMFONY_GRAPHEME_CLUSTER_RX.')/u', $s, $matches)) { | ||
return false; | ||
} | ||
|
||
if (1 === $len) { | ||
return $matches[0]; | ||
} | ||
|
||
$chunks = array_chunk($matches[0], $len); | ||
|
||
foreach ($chunks as &$chunk) { | ||
$chunk = implode('', $chunk); | ||
} | ||
|
||
return $chunks; | ||
} | ||
|
||
private static function grapheme_position($s, $needle, $offset, $mode) | ||
{ | ||
$needle = (string) $needle; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters