tags:

views:

32

answers:

1

I am storing some text values in memcached. An example of such a string is:

El Salvador (República De El Salvador)

When I attempt to retrieve the string, I get an empty string. I suspect this is something to do with UTF-8 strings. How may I store foreign language text in memcached?

+2  A: 

I'm not a Unicode expert, but this piqued my curiosity so I wrote a little code:

http://static.bwerp.net/~adam/20100420/m.php

Seems to work reasonably well, over here. You said you thought it was a UTF-8 issue. What happens if you remove the accented character?

For posterity, here's the code in the linked file:

header('Content-type: text/html; charset=utf-8');

echo '<pre>';

$s = utf8_encode("El Salvador (Rep\xfablica De El Salvao)");
echo '$s = ', $s, '<br>';
var_dump( mb_detect_encoding($s) );

$m = new Memcache;
$m->addServer('localhost', 11211);

var_dump( $m->set('foobarcheeze', $s) );
var_dump( $m->get('foobarcheeze') );

echo 'strlen($s) = ', strlen($s), '<br>';
echo 'mb_strlen($s) = ', mb_strlen($s), '<br>';
echo 'mb_strwidth($s) = ', mb_strwidth($s), '<br>';
Adam Backstrom
@adam: Thanks, for your input. I may have been barking up the wrong tree. I have other variables that I can retrieve without any issues from memcache. On first inspection, it appears all the missing strings are those with accentuated chars. I'll take a look at this later on this eve and let you know.
morpheous
@adam: Thanks. It was the header directive that made all the difference. I am accepting your answer now.
morpheous