note 108686 modified in function.strtolower by danbrown

From: Date: Thu, 17 May 2012 17:47:44 +0000
Subject: note 108686 modified in function.strtolower by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188039@lists.php.net to get a copy of this message
Everyone knows that strtolower/strtoupper does not translate cyrillic charactes (cp1251 code page). But I just was surprised that strtolower works twice(!) slower that translating characters with "strtr()".

Here is what I use to translate cyrillic (works for Russian, Ukrainian, Belorussian and cyrillic balcan languages, like Serbian etc):

<?php
// Arrays (REMOVE THE LINE BREAKS! THIS NOT WON'T BE POSTED WITH SUCH LONG LINES!)
$chars_hi = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ
АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯЁЂЃЄЅІЇЈЉЊЋЌЎЏҐ';
$chars_lo = 'abcdefghijklmnopqrstuvwxyz
абвгдежзийклмнопрстуфхцчшщъыьэюяёђѓєѕіїјљњћќўџґ';

// functions
function uppercase($s) {
 global $chars_hi, $chars_lo;
 return strtr($s, $chars_lo, $chars_hi);
}

function lowercase($s) {
 global $chars_hi, $chars_lo;
 return strtr($s, $chars_hi, $chars_lo);
}
?>

Remember, it works TWICE FASTER than strtoupper/strtolower! So maybe someone will would like to translate the arrays for another code pages different than cp1251 --was-- Everyone knows that strtolower/strtoupper does not translate cyrillic charactes (cp1251 code page). But I just was surprised that strtolower works twice(!) slower that translating characters with "strtr()". Here is what I use to translate cyrillic (works for Russian, Ukrainian, Belorussian and cyrillic balcan languages, like Serbian etc): // Arrays (REMOVE THE LINE BREAKS! THIS NOT WON'T BE POSTED WITH SUCH LONG LINES!) $chars_hi = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯЁЂЃЄЅІЇЈЉЊЋЌЎЏҐ'; $chars_lo = 'abcdefghijklmnopqrstuvwxyz абвгдежзийклмнопрстуфхцчшщъыьэюяёђѓєѕіїјљњћќўџґ'; // functions function uppercase($s) { global $chars_hi, $chars_lo; return strtr($s, $chars_lo, $chars_hi); } function lowercase($s) { global $chars_hi, $chars_lo; return strtr($s, $chars_hi, $chars_lo); } Remember, it works TWICE FASTER than strtoupper/strtolower! So maybe someone will would like to translate the arrays for another code pages different than cp1251 http://php.net/manual/en/function.strtolower.php

« previous php.notes (#188039) next »