Pyh.conf’25: a new PHP conference for the Russian-speaking community

Voting

: five minus three?
(Example: nine)

The Note You're Voting On

Tedy
12 years ago
Since strtr() is twice faster than strlwr I decided to write my own lowering function which also handles UTF-8 characters.

<?php

function strlwr($string, $utf = 1)
{
$latin_letters = array('Ă' => 'a',
'Â' => 'a',
'Î' => 'i',
'Ș' => 's',
'Ş' => 's',
'Ț' => 't',
'Ţ' => 't');

$utf_letters = array('Ă' => 'ă',
'Â' => 'â',
'Î' => 'î',
'Ș' => 'ș',
'Ş' => 'ş',
'Ț' => 'ț',
'Ţ' => 'ţ');

$letters = array('A' => 'a',
'B' => 'b',
'C' => 'c',
'D' => 'd',
'E' => 'e',
'F' => 'f',
'G' => 'g',
'H' => 'h',
'I' => 'i',
'J' => 'j',
'K' => 'k',
'L' => 'l',
'M' => 'm',
'N' => 'n',
'O' => 'o',
'P' => 'p',
'Q' => 'q',
'R' => 'r',
'S' => 's',
'T' => 't',
'U' => 'u',
'V' => 'v',
'W' => 'w',
'X' => 'x',
'Y' => 'y',
'Z' => 'z');

return (
$utf == 1) ? strtr($string, array_merge($utf_letters, $letters)) : strtr($string, array_merge($latin_letters, $letters));
}

?>

This allows you to lower every character (even UTF-8 ones) if you don't set the second parameter, or just lower the UTF-8 ones into their specific latin characters (used when making friendly-urls for example).

I used romanian characters but, of course, you can add your own local characters.

Feel free to use/modify this function as you wish. Hope it helps.

<< Back to user notes page

To Top