How to flip a string? It was Hello - it became TevirP .
Do not offer the standard function - strrev() does not work with utf-8 encoding.
Options that made:

  1. Use mbstring. Passing a loop from the end of the line, we get letters through mb_substr() , we form the final line.
  2. Use regular expressions. Fetching characters through preg_match_all() into an array, doing array_reverse() and implode() .

Tell me more options for algorithms / implementations of such a transformation.

  • 2
    strrev - supports UTF - Alexander Semikashev
  • one
    @AlexanderSemikashev where is it written? - u_mulder
  • @u_mulder, and where is it written that does not support? - Dmitriy
  • The manual is not written. Actually, the answer below is an option from the comments. - u_mulder
  • one
    strrev supports all encodings because it does not work with them. the function inverts the byte order, and how higher a client interprets these bytes a level — the client's problem. therefore, the statement “supports utf” and any other type “supports <insert any value>” is true - Lexx918

2 answers 2

 function mb_strrev($text) { return join('', array_reverse(preg_split('//u', $text, -1, PREG_SPLIT_NO_EMPTY))); } echo mb_strrev('☆❤world'); 

Well, and as an option you can consider:

 function mb_strrev($string) { $string = strrev(mb_convert_encoding($string, 'UTF-16BE', 'UTF-8')); return mb_convert_encoding($string, 'UTF-8', 'UTF-16LE'); } echo mb_strrev('☆❤world'); 
  • @ Edward, thank you, did not notice .. - Let's say Pie
  • Thanks, with preg_split() did not try, you need to test the effectiveness compared with preg_match_all() . Let's wait for some more other solutions to the problem. - iapetus
  • @iapetus, added another answer - Let's say Pie

http://php.net/manual/ru/function.strrev.php#122953

 function mb_strrev($str){ $r = ''; for ($i = mb_strlen($str); $i>=0; $i--) { $r .= mb_substr($str, $i, 1); } return $r; } echo mb_strrev("☆❤world"); // echo "dlrow❤☆" ?> 
  • Thanks for the answer, but this implementation is the first item in my question. - iapetus