Архив

Архив Апрель 2011
15 апреля 2011 14 комментариев

Раньше пользовался функцией:

function GetInTranslit($string) {
	$string=strtr($string,"абвгдеёзийклмнопрстуфхцыэАБВГДЕЁЗИЙКЛМНОПРСТУФХЦЫЭ","abvgdeeziyklmnoprstufhcieABVGDEEZIKLMNOPRSTUFHCIE");
	$string=strtr($string, array(
		"ж"=>"zh", " "=>"-", "ч"=>"ch", "ш"=>"sh",
		"щ"=>"shch", "ь"=>"", "ъ"=>"", "ю"=>"yu", "я"=>"ya",
		"Ж"=>"Zh", "Ч"=>"Ch", "Ш"=>"Sh",
		"Щ"=>"Shch", "Ь"=>"", "Ъ"=>"", "Ю"=>"Yu", "Я"=>"Ya",
		"і"=>"i", "І"=>"I", "ї"=>"yi", "Ї"=>"Yi", "є"=>"ie", "Є"=>"Ye")
	);
return $string;
}

но, когда текст в UTF-8 выходят крокозяблы. :( Правильный вариант:

function GetInTranslit($string) {
	$replace=array(
		"'"=>"",
		"`"=>"",
		"а"=>"a","А"=>"a",
		"б"=>"b","Б"=>"b",
		"в"=>"v","В"=>"v",
		"г"=>"g","Г"=>"g",
		"д"=>"d","Д"=>"d",
		"е"=>"e","Е"=>"e",
		"ж"=>"zh","Ж"=>"zh",
		"з"=>"z","З"=>"z",
		"и"=>"i","И"=>"i",
		"й"=>"y","Й"=>"y",
		"к"=>"k","К"=>"k",
		"л"=>"l","Л"=>"l",
		"м"=>"m","М"=>"m",
		"н"=>"n","Н"=>"n",
		"о"=>"o","О"=>"o",
		"п"=>"p","П"=>"p",
		"р"=>"r","Р"=>"r",
		"с"=>"s","С"=>"s",
		"т"=>"t","Т"=>"t",
		"у"=>"u","У"=>"u",
		"ф"=>"f","Ф"=>"f",
		"х"=>"h","Х"=>"h",
		"ц"=>"c","Ц"=>"c",
		"ч"=>"ch","Ч"=>"ch",
		"ш"=>"sh","Ш"=>"sh",
		"щ"=>"sch","Щ"=>"sch",
		"ъ"=>"","Ъ"=>"",
		"ы"=>"y","Ы"=>"y",
		"ь"=>"","Ь"=>"",
		"э"=>"e","Э"=>"e",
		"ю"=>"yu","Ю"=>"yu",
		"я"=>"ya","Я"=>"ya",
		"і"=>"i","І"=>"i",
		"ї"=>"yi","Ї"=>"yi",
		"є"=>"e","Є"=>"e"
	);
	return $str=iconv("UTF-8","UTF-8//IGNORE",strtr($string,$replace));
}

на основе http://sono-design.ru/blog/programming/translit-v-utf-8#

Добавлено 19.01.2017
Оптимально для формирования ЧПУ:

function translit($str){
	$translit=array(
		"А"=>"a","Б"=>"b","В"=>"v","Г"=>"g","Д"=>"d","Е"=>"e","Ё"=>"e","Ж"=>"zh","З"=>"z","И"=>"i","Й"=>"y","К"=>"k","Л"=>"l","М"=>"m","Н"=>"n","О"=>"o","П"=>"p","Р"=>"r","С"=>"s","Т"=>"t","У"=>"u","Ф"=>"f","Х"=>"h","Ц"=>"ts","Ч"=>"ch","Ш"=>"sh","Щ"=>"shch","Ъ"=>"","Ы"=>"y","Ь"=>"","Э"=>"e","Ю"=>"yu","Я"=>"ya",
		"а"=>"a","б"=>"b","в"=>"v","г"=>"g","д"=>"d","е"=>"e","ё"=>"e","ж"=>"zh","з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l","м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r","с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h","ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"shch","ъ"=>"","ы"=>"y","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya",
		"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"
	);
	$result=strtr($str,$translit);
	$result=preg_replace("/[^a-zA-Z0-9_]/i","-",$result);
	$result=preg_replace("/\-+/i","-",$result);
	$result=preg_replace("/(^\-)|(\-$)/i","",$result);
	return $result;
}
Categories: PHP Tags:
6 апреля 2011 Нет комментариев
<?php
$array = array(1, 2, 3); //некий массив
$array_serialized = serialize($array); //эта переменная - текстовая, но содержит наш массив, записанный своим "кодом"
mysql_query("UPDATE TABLE `table` SET `field` = '".$array_serialized."'"); //записываем её в таблицу
$sql = mysql_query("SELECT * FROM `field`"); //читаем SQL данные
$temp = mysql_fetch_assoc($sql);
$array = unserialize( $temp['field'] ); //получаем свой массив.
?>

http://www.drupal.ru/node/33788

Categories: MySQL, PHP Tags: ,