Posts Tagged ‘encoding’

strtolower and encoding

Saturday, July 10th, 2010

A PHP problem that tends to sneak by is the handling of strtolower and encoding. For a while now I have worked on a project that is partially on a Windows-server, with limited control for us. Sometimes there is trouble when you make comparison of textstrings and want to use strtolower. Since “Mattias” and “mattias” is not the same thing you want to make both strings lowercse. But strtolower() don’t want to play with for example swedish chars "å,ä,ö" in UTF-8.

A solution to this problem can be to change the chars to something else, why not htmlentities(). You can for example to this to change the value to lowercase and get around encoding problem:

1
2
3
$value = htmlentities($value, ENT_COMPAT, 'UTF-8');
$value = trim(strtolower($value));
$value = html_entity_decode($value, ENT_COMPAT, 'UTF-8');

Another solutions is to use mb_strtolower() if that function is available (which it isn't on the Windows server in question).

Geek dreams – A world of UTF-8

Monday, July 5th, 2010

In my geek dreams are a world of UTF-8 (or UTF-16 maybe). Wouldn’t it be nice to have no more problems with encoding, weird question-marks and characters gone forever. No more problems where the database output sorts Ö before O (ö is the last letter in the Swedish alphabet). Forget ut8_encode and iconv. Such a great place the world could be…