9

I'm trying to make a code that replace Arabic text to be supported in non Arabic supported programs
in that i will be need to reverse the text after replace but its shows some garbage stuff instead of the wanted result

Here Is The Code :

<?php
$string = "اهلا بك";
echo "$string <br>";
$Reversed = strrev($string);
echo "<br><b>After Reverse</b><br><br>";
echo "<br> $Reversed";
?>

Result :

اهلا بك

After Reverse


�٨� �؄ه٧

I need it to be the way it is but reversed ? not GARBAGE !!

Ali Almoullim
  • 1,028
  • 9
  • 30
  • Although you've answered your own question, there are more functions that need to be replaced to be able to handle UTF-8 properly. http://stackoverflow.com/questions/16858915/migrating-a-php-application-to-handle-utf-8/16862181#16862181 – Danack Jul 05 '13 at 22:48

4 Answers4

14

in order to make that strrev() support UTF-8 you need to use this Function

function utf8_strrev($str){
    preg_match_all('/./us', $str, $ar);
    return join('', array_reverse($ar[0]));
}

so we going to chage strrev() in our code to utf8_strev() :

$string = "اهلا بك";
echo "$string <br>";
$Reversed = utf8_strrev($string); // here we have changed it
echo "<br><b>After Reverse</b><br><br>";
echo "<br> $Reversed";

and the Result is :

اهلا بك

After Reverse


كب الها
Ali Almoullim
  • 1,028
  • 9
  • 30
  • 1
    Admittedly, I don't speak Arabic; but that doesn't really look like the same characters to me – Mark Baker Jul 05 '13 at 20:40
  • @MarkBaker it is, but in Arabic when we put character together its make it looks different wich from its place in the back or in the front of the word and even if its in the middle – Ali Almoullim Jul 05 '13 at 20:44
  • Strange: I'm sure it's logical if you're used to it though... I guess Greek has some vestiges of that, with σ when used at the beginning or in the middle or a word, and ς when used at the end – Mark Baker Jul 05 '13 at 20:46
  • Regardless of looking different or not, it's a useful trick and one worth remembering: many of the string functions can be overloaded to make them work with UTF-8 by setting `mbstring.func_overload`, but strrev() isn't one of those – Mark Baker Jul 05 '13 at 20:47
  • Oh, I trust you - logically it should work exactly as you've described (and I think it's a great solution to the problem): it just feels strange to me as a non-Arabic speaker/reader – Mark Baker Jul 05 '13 at 20:50
  • I benchmarked this function against @dav's in terms of performance, this one is slightly faster. – evilReiko Mar 13 '19 at 06:26
2

A more generic solution that handles all encodings, not only UTF-8:

function mb_strrev ($string, $encoding = null)
{
    if ( is_null($encoding) ) {
        $encoding = mb_detect_encoding($string);
    }

    $length   = mb_strlen($string, $encoding);
    $reversed = '';

    while ( $length-->0 ) {
        $reversed .= mb_substr($string, $length, 1, $encoding);
    }

    return $reversed;
}

Thanks to Kevin van Zonneveld

dotancohen
  • 30,064
  • 36
  • 138
  • 197
Moshe Dolev
  • 194
  • 2
  • 4
2

I have been using this one

taken from here http://php.net/manual/en/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;
}
dav
  • 8,931
  • 15
  • 76
  • 140
  • Correct. Just change `$i = mb_strlen($str);` to `$i = mb_strlen($str) - 1;`, as first iteration concatenates empty string unnecessarily. – evilReiko Mar 13 '19 at 06:20
-1
$my_string = 'Очень длинный-длинный текст :)'; 

function user_reverse($str){    // name can be arbitrary
   
    $arr = mb_str_split($str);
            
    return join('',array_reverse($arr));
 }

 echo (user_reverse($my_string));  // ): тскет йыннилд-йыннилд ьнечО*
  • 4
    Please explain your solution. Answers which do not have an explanation and are only code get flagged as low effort. – cursorrux Sep 07 '21 at 17:12
  • Especially because there's already an accepted answer... – Sven Eberth Sep 08 '21 at 00:21
  • @Sven the fact that there is an accepted answer is not a factor in reviewing the quality of this answer. All answers should be explained with the intent to educate/empower the OP and thousands of future researchers. In truth it looks like this answer is a copy of the accepted answer with the exception that `mb_str_split()` is called instead of `preg_match_all()`. – mickmackusa Dec 26 '21 at 22:11