<?php

// конвертер кодировки
abstract class convert{

// конвертируем из UTF в нужную кодировку
static function of_utf8($str,$to='cp1251')
{
if (self::charset($str)=='UTF-8'){
if (function_exists('mb_substr'))return mb_convert_encoding($str, $to, 'UTF-8');
if (function_exists('iconv'))return iconv('UTF-8', $to, $str);
}
return $str;
}



// конвертируем в UTF
static function to_utf8($str,$from='cp1251')
{
if (self::charset($str)==$from){
if (function_exists('mb_substr'))return mb_convert_encoding($str, 'UTF-8', $from);
if (function_exists('iconv'))return iconv($from, 'UTF-8', $str);
}
return $str;
}


static function charset($str)
{
if (preg_match('#[а-яёïієґ]+#ui', $str))return 'UTF-8';
else return 'cp1251';
}



static function translit($str)
{
$trans1= array("'",'`',',',' ',"Ё","Ж","Ч","Ш","Щ","Э","Ю","Я","ё","ж","ч","ш","щ","э","ю","я","А","Б","В","Г","Д","Е","З","И","Й","К","Л","М","Н","О","П","Р","С","Т","У","Ф","Х","Ц","Ь","Ы","а","б","в","г","д","е","з","и","й","к","л","м","н","о","п","р","с","т","у","ф","х","ц","ь","ы");
$trans2= array('_','_','_','_',"JO","ZH","CH","SH","SCH","Je","Jy","Ja","jo","zh","ch","sh","sch","je","jy","ja","A","B","V","G","D","E","Z","I","J","K","L","M","N","O","P","R","S","T","U","F","H","C","","Y","a","b","v","g","d","e","z","i","j","k","l","m","n","o","p","r","s","t","u","f","h","c","`","y");
return str_replace($trans1,$trans2,$str);

}

}


?>
