grapheme_strrev
grapheme_strrev(string $string): string
Reverses a string by grapheme cluster (new in PHP 8.6, Intl): emoji ZWJ sequences and combining marks stay intact.
C C23
#include <stdio.h>
#include <string.h>
int main(void) {
/* No grapheme segmentation in the C stdlib — reverse an ASCII byte string only */
char s[] = "abcdef";
size_t i = 0, j = strlen(s) - 1;
while (i < j) {
char t = s[i]; s[i] = s[j]; s[j] = t;
i++; j--;
}
printf("%s\n", s);
/* Multibyte & grapheme cases (CJK, combining marks, ZWJ emoji) are out of
hand-reversing scope — real code needs ICU or libunistring */
return 0;
}
fedcba
C++ C++26
#include <algorithm>
#include <iostream>
#include <string>
int main() {
// No grapheme segmentation in the C++ stdlib — reverse an ASCII string only
std::string s = "abcdef";
std::reverse(s.begin(), s.end());
std::cout << s << "\n";
// Grapheme-level reversal needs ICU; byte-wise tricks tear CJK/emoji apart
return 0;
}
fedcba
C# v11.0.100
using System.Globalization;
// StringInfo.GetTextElementEnumerator walks text elements (grapheme clusters)
Console.WriteLine(GraphemeReverse("abcdef"));
Console.WriteLine(GraphemeReverse("你好世界"));
Console.WriteLine(GraphemeReverse("e\u0301abc")); // combining mark stays attached
Console.WriteLine(GraphemeReverse("a👨👩👧👦b")); // ZWJ family emoji = ONE element
static string GraphemeReverse(string s)
{
var en = StringInfo.GetTextElementEnumerator(s);
var parts = new List<string>();
while (en.MoveNext()) parts.Add((string)en.Current);
parts.Reverse();
return string.Join("", parts);
}
fedcba 界世好你 cbaé b👨👩👧👦a
Go v1.27.4
package main
import "fmt"
func main() {
// No grapheme segmentation in the Go stdlib — []rune reverses by Unicode code point
s := []rune("你好世界")
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
fmt.Println(string(s))
// Code-point reversal is safe while every character is one scalar (CJK),
// but combining marks & ZWJ emoji tear — grapheme level needs rivo/uniseg
}
界世好你
Java v27.0.1
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
// BreakIterator.getCharacterInstance walks "character" boundaries ≈ grapheme clusters
static String graphemeReverse(String s) {
BreakIterator it = BreakIterator.getCharacterInstance();
it.setText(s);
List<String> parts = new ArrayList<>();
int start = it.first();
for (int end = it.next(); end != BreakIterator.DONE; start = end, end = it.next()) {
parts.add(s.substring(start, end));
}
Collections.reverse(parts);
return String.join("", parts);
}
public static void main(String[] args) {
System.out.println(graphemeReverse("abcdef"));
System.out.println(graphemeReverse("你好世界"));
System.out.println(graphemeReverse("e\u0301abc"));
System.out.println(graphemeReverse("a👨👩👧👦b"));
}
}
fedcba 界世好你 cbaé b👨👩👧👦a
JavaScript v26.13.1
// Intl.Segmenter (ES2022, granularity "grapheme"): the native grapheme cutter
const seg = new Intl.Segmenter("en", { granularity: "grapheme" });
const rev = (s) => Array.from(seg.segment(s), (x) => x.segment).reverse().join("");
console.log(rev("abcdef"));
console.log(rev("你好世界"));
console.log(rev("e\u0301abc")); // combining mark stays attached
console.log(rev("a👨👩👧👦b")); // ZWJ family emoji = ONE segment
fedcba 界世好你 cbaé b👨👩👧👦a
PHP v8.6.0
// grapheme_strrev (PHP 8.6+ Intl): reverse by grapheme cluster — user-perceived characters stay whole
echo grapheme_strrev("abcdef"), "\n";
echo grapheme_strrev("你好世界"), "\n";
echo grapheme_strrev("café"), "\n";
echo grapheme_strrev("e\u{0301}abc"), "\n";
echo grapheme_strrev("a👨👩👧👦b"), "\n";
// Contrast: strrev flips bytes — multibyte characters get torn into garbage
echo bin2hex(strrev("你好")), "\n";
fedcba 界世好你 éfac cbaé b👨👩👧👦a bda5e5a0bde4
Python v3.15.2
# No grapheme segmentation in the Python stdlib — strings are code-point sequences
text = "你好世界"
print(text[::-1]) # safe while every character is a single code point (CJK)
# Combining marks & ZWJ emoji tear at code-point level; the third-party
# regex module's \X match or grapheme libraries are the way — stdlib has none
界世好你
Rust v1.99.1
use unicode_segmentation::UnicodeSegmentation;
fn main() {
// std has no grapheme segmentation — unicode-segmentation's graphemes() is the norm
let rev = |s: &str| s.graphemes(true).rev().collect::<String>();
println!("{}", rev("abcdef"));
println!("{}", rev("你好世界"));
println!("{}", rev("e\u{0301}abc")); // combining mark stays attached
println!("{}", rev("a👨👩👧👦b")); // ZWJ family emoji = ONE grapheme
}
fedcba 界世好你 cbaé b👨👩👧👦a
TypeScript v26.13.1
// Intl.Segmenter (ES2022, granularity "grapheme"): the native grapheme cutter
const seg = new Intl.Segmenter("en", { granularity: "grapheme" });
const rev = (s: string): string => Array.from(seg.segment(s), (x) => x.segment).reverse().join("");
console.log(rev("abcdef"));
console.log(rev("你好世界"));
console.log(rev("e\u0301abc")); // combining mark stays attached
console.log(rev("a👨👩👧👦b")); // ZWJ family emoji = ONE segment
fedcba 界世好你 cbaé b👨👩👧👦a
Language differences
String reversal in three tiers: bytes (strrev) → code points (chars/[]rune) → grapheme clusters — the user-perceived "one character" only exists at the third tier; pairs with the strlen/mb_strlen tiering.
C: no grapheme segmentation (the example reverses ASCII bytes only); combining marks & ZWJ emoji are beyond reasonable hand-writing — ICU or libunistring in real code.
C++: likewise no grapheme segmentation (std::reverse on ASCII only); ICU is the practical choice.
C#: StringInfo.GetTextElementEnumerator walks text elements (≈ grapheme clusters) natively (the example reverses the ZWJ family emoji intact).
Go: no grapheme segmentation in the stdlib — []rune reversal is code-point level (safe Chinese in the example, one code point per character); combining sequences tear; rivo/uniseg in real code.
Java: BreakIterator.getCharacterInstance's "character boundaries" ≈ extended grapheme clusters (ZWJ emoji intact in the example); precision tracks the bundled Unicode version of UAX #29.
JavaScript: Intl.Segmenter (ES2022) with granularity "grapheme" segments natively (ZWJ emoji intact).
PHP: strrev flips bytes and shreds multibyte characters (the hex line shows it raw); grapheme_strrev (new in 8.6, Intl) reverses by grapheme cluster — correct at the user-perceived tier; needs the intl extension.
Python: no grapheme segmentation in the stdlib (code-point reversal in the example, safe for single-scalar characters); combining sequences need the third-party regex module's \X or a grapheme library.
Rust: no grapheme segmentation in std — unicode-segmentation's graphemes() is the de facto standard (ZWJ emoji intact); chars() is code-point level only.
TypeScript: same as JavaScript (Intl.Segmenter).