Ahmad Farid
Ahmad Farid

Reputation: 14774

Problem in UTF Encoding in PHP

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.

Upvotes: 0

Views: 1021

Answers (3)

Vadym
Vadym

Reputation: 747

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

Upvotes: 3

Floern
Floern

Reputation: 33904

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.

Upvotes: 0

Dan Grossman
Dan Grossman

Reputation: 52372

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

Upvotes: 4

Related Questions