Posted By

iTony on 10/12/12


Tagged

php unicode multibyte


Versions (?)

Reverse a multibyte string in PHP


 / Published in: PHP
 

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

It is a bit simpler reversing of multibyte strings.

  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. }

Report this snippet  

You need to login to post a comment.