2, // πŸ˜€πŸ˜€ "\u{1F469}\u{200D}\u{1F4BB}" => 1, // πŸ‘©β€πŸ’» "\u{1F469}\u{200D}\u{1F4BB}\u{1F469}\u{200D}\u{1F4BB}" => 2, // πŸ‘©β€πŸ’»πŸ‘©β€πŸ’» "\u{1F1EA}\u{1F1F8}\u{1F1EC}\u{1F1E7}" => 2, // πŸ‡ͺπŸ‡ΈπŸ‡¬πŸ‡§ "e\u{0301}" => 1, // Γ© with a combining accent. ); foreach ( $checks as $text => $expected ) { if ( preg_match_all( '/\X/u', $text ) !== $expected ) { return false; } } return true; } /** * Graphemes of a valid UTF-8 text with `\X` or the fallback pattern. * * @param string $text Text. * @return string[] */ private static function split( $text ) { $pattern = 'pcre' === self::method() ? '/\X/u' : self::FALLBACK; return preg_match_all( $pattern, $text, $matches ) ? $matches[0] : array(); } /** * Whether a string is valid UTF-8. * * @param string $text Text. * @return bool */ private static function is_utf8( $text ) { return 1 === preg_match( '//u', $text ); } }