Re: strrev() for persian language!
| From: | Maciek Sokolewicz | Date: | Mon, 29 Sep 2008 10:56:25 +0000 |
| Subject: | Re: strrev() for persian language! | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-281084@lists.php.net to get a copy of this message | ||
shahrzad khorrami wrote:
hi all, for reversing of characters, strrev() is good. but for persian characters it doesn't work! ThanksHow nice of you to let us know! Now, is there a question in here somewhere? or is that all you wanted to say? -- _assuming_ you want to know why this is: Persian characters are represented using > 1 byte per character. strrev() assumes exactly 1 byte per character, and just reverses the "string" of bytes. Which results in a reversed string. (some) Persian characters are represented using 2 (or more?) bytes; just reversing those will lead to an entirely different chracter (or comination thereof). _assuming_ you want to know how to get around this: Use a function which handles these cases properly. One that is encoding-aware (and doesn't just assume a 1-byte charset). You can either use a combination of iconv_* functions to do this, or use a combination of mb_* functions. Something like this should work: function iconv_strrev($string, $encoding) { $return = ''; $len = iconv_strlen($string, $encoding); for($i=($len-1);$i >= 0;$i--) {
$return .= iconv_substr($string, $i, 1, $encoding);} return $return; }