Reverse a multibyte string in PHP


/ Published in: PHP
Save to your folder(s)

It is a bit simpler reversing of multibyte strings.


Copy this code and paste it in your HTML
  1. <?php
  2. function mb_strev ($string, $encoding = null) {
  3. if ($encoding === null) {
  4. $encoding = mb_detect_encoding($string);
  5. }
  6.  
  7. $length = mb_strlen($string, $encoding);
  8. $reversed = '';
  9. while($length-- > 0) {
  10. $reversed .= mb_substring($string, $length, 1, $encoding);
  11. }
  12.  
  13. return $reversed;
  14. }

URL: http://kvz.io/blog/2012/10/09/reverse-a-multibyte-string-in-php/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.