Skip to content

Instantly share code, notes, and snippets.

@lovelock
Created June 22, 2018 08:22
Show Gist options
  • Save lovelock/980e1f26a432bae7bf23f0ea56474492 to your computer and use it in GitHub Desktop.
Save lovelock/980e1f26a432bae7bf23f0ea56474492 to your computer and use it in GitHub Desktop.
reverse multibyte strings in PHP
<?php
// http://kvz.io/blog/2012/10/09/reverse-a-multibyte-string-in-php/
function mb_strrev ($string, $encoding = null) {
if ($encoding === null) {
$encoding = mb_detect_encoding($string);
}
$length = mb_strlen($string, $encoding);
$reversed = '';
while ($length-- > 0) {
$reversed .= mb_substr($string, $length, 1, $encoding);
}
return $reversed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment