0

I use the following lines of code:

$revTerm =  "". strrev($limitAry["term"]);
$revTerm = utf8_encode($revTerm);

The $revTerm contains Norwegian characters as ø æ å. However, it is shown correctly. I need to reverse them before displaying, so I use the first line. When I display them this way, I get an error of bad xml format - used to fill a grid. When I try to use the second line, I don't get an error but the characters are not shown correctly. Could there be any other way to solve that? If it may help, I use jqGrid to fill those data in.

3
  • 1
    As you use utf8_encode, the string in $limitAry["term"] is encoded with ISO 8859-1, right? Commented Feb 7, 2011 at 9:17
  • The string is already destroyed before he calls utf8_encode. Since strrev is written for single-byte encodings, it swaps the order of bytes that were part of single multi-byte characters, effectively turning them into different characters or nonsensical byte sequences. Commented Feb 7, 2011 at 9:22
  • @Dan Grossman: I just don’t see the point for utf8_encode. If $limitAry["term"] were ISO 8859-1 (or any other single-byte character encoding), strrev would work and utf8_encode would convert that reversed string to UTF-8 (including incorrect mapping if $limitAry["term"] is not ISO 8859-1). But if $limitAry["term"] already were UTF-8, strrev would not work properly and using utf8_encode wouldn’t make any sense as it’s already UTF-8. But none of these scenarios explain the XML error. Commented Feb 7, 2011 at 9:48

3 Answers 3

4

strrev, like most PHP string functions, is not safe for multi-byte encodings.

Sign up to request clarification or add additional context in comments.

2 Comments

To be fair, it can be used for UCS-2 and UCS-4, however it changes the endianness of the string.
You'll have to write it yourself, right, and only make use of the functions in PHP's multibyte library (which doesn't include a built-in string reversing function): php.net/manual/en/book.mbstring.php
3

try this example

$test = 'А роза упала на лапу Азора ウィキ';
$test = iconv('utf-8', 'utf-16le', $test);
$test = strrev($test);
// キィウ арозА упал ан алапу азор А
echo iconv('utf-16be', 'utf-8', $test);

(russian) http://bolknote.ru/2012/04/02/~3625#56

1 Comment

Short explanation for people, who don't understand Russian: this will correctly reverse order of most multibyte string as long as they consist from single-codepoint symbols. It abuses the fact, that byte-by-byte reversion of UTF-16 produces correctly reversed UTF-16 string, but in different endianess. It won't work for complex graphemes (such as most emojis, certain forms of characters with accents etc). A handy article about the multi-codepoint symbols: developer.apple.com/library/content/documentation/Cocoa/…
0

Try this:

$revTerm = utf8_decode($limitAry["term"]);
$revTerm = strrev($revTerm);
$revTerm = utf8_encode($revTerm);

For using strrev you have to decode your string to a non-multibyte string.

1 Comment

That only works if all the characters in the string exist in the ISO-8859-1 character set

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.