Reverse a string: Difference between revisions

→‎{{header|PHP}}: unicode and graphemes
(→‎{{header|PHP}}: unicode and graphemes)
Line 3,486:
 
=== Unicode ===
 
==== Code points ====
 
If you want Unicode support, you have to use some multibyte function. Sadly, PHP doesn't contain <code>mb_strrev()</code>. One of functions which support Unicode and is useful in this case is <code>preg_split()</code>.
Line 3,496 ⟶ 3,498:
 
<syntaxhighlight lang="php">
implode('', array_reverse(mb_str_split($string))); // fed 🐧 cba
</syntaxhighlight>
 
==== Graphemes ====
 
When using combining characters such as diacritics or ZWJ (joining), reversing code points will mess with the result, reversing the graphemes instead is mandatory. This is generally the best and safest approach. As there is no <code>grapheme_reverse()</code> function or grapheme iterator, one has to implement it with <code>grapheme_strlen</code> and <code>grapheme_substr</code>. In PHP, there is no Unicode escape sequence so to specify characters by code point a tricks must be used: for example, using the escape sequence of HTML entities and then convert it to a Unicode encoding such as UTF-8.
 
<syntaxhighlight lang="php">
$a = mb_convert_encoding('&#x1F466;&#x1F3FB;&#x1f44b;', 'UTF-8', 'HTML-ENTITIES'); // 👦🏻👋
 
function str_to_array($string)
{
$length = grapheme_strlen($string);
$ret = [];
 
for ($i = 0; $i < $length; $i += 1) {
 
$ret[] = grapheme_substr($string, $i, 1);
}
 
return $ret;
}
 
function utf8_strrev($string)
{
return implode(array_reverse(str_to_array($string)));
}
 
print_r(utf8_strrev($a)); // 👋👦🏻
</syntaxhighlight>