Webmaster
 -> 
For Coders
 -> 
Scripts
 -> 
PHP: Convert cyrillic text
 
... 

PHP: Convert cyrillic text

defining text code tabel and convert it to required

function convert_cyr_text($text,$convert_to=""){                                 // detect codepage of $text and return converted to $convert_to
// Swed  http://collection.com.ua/webmaster
  if(!$code_page)$convert_to = "w";                                              // by default convert to windows-1251
  $countChars = count_chars($text);                                              // cont chars of text
  $sums = array(                                                                 // set sums for each codepage
   "w" => array("small" => array_sum(array_slice($countChars,224,32)),           // windows-1251 small chars
                "cap"   => array_sum(array_slice($countChars,192,32))),          // capitalize chars
   "k" => array("small" => array_sum(array_slice($countChars,192,32)),           // koi8-r
                "cap"   => array_sum(array_slice($countChars,224,32))),
   "i" => array("small" => array_sum(array_slice($countChars,208,32)),           // iso8859-5
                "cap"   => array_sum(array_slice($countChars,176,32))),
   "a" => array("small" => array_sum(array_slice($countChars,160,16))+           // cp866
                             array_sum(array_slice($countChars,224,16)),
                "cap"   => array_sum(array_slice($countChars,128,32))),
   "m" => array("small" => array_sum(array_slice($countChars,223,32)),           // x-mac-cyrillic
                "cap"   => array_sum(array_slice($countChars,128,32))),
  );
  $max = max($sums);                                                             // get max values
  foreach ($sums as $name => $codepage)
   if ($codepage == $max) $convert_from = $name;                                 // get name current codepage
  if ($convert_from == $convert_to) return $text;                                // retrun not converted
  return convert_cyr_string($text,$convert_from,$convert_to);                    // return converted
}

 

 

... 
Site Map
News
Search






 Recomended screen size 1024x768 http://www.collection.com.ua webmaster © 2002-2006