Поискав информацию с разных источников написал функцию перевода любого текста к виду
, но к сожалению эта
функция не всегда справляется верно, подскажите пожалуйста где ошибка.
function formatName($text)
{
$text = mysql_real_escape_string($text);
$text = preg_replace("/(\r\n){2,}/", "\r\n", $text);
$text = str_replace('#39;', '', $text);
$text = str_replace('"', '', $text);
$text = str_replace('"', '', $text);
$text = str_replace('&', '', $text);
$text = preg_replace('/([?!:^~|@№$–=+*&%.,;\[\]<>()_—«»#\/]+)/', '', $text);
$text = trim($text);
$text = preg_replace("/ +/", " ", $text);
$text = preg_replace('/ +/', '-', $text);
$trans = array("а"=>"a","А"=>"a","б"=>"b","Б"=>"b","в"=>"v","В"=>"v","г"=>"g",
"Г"=>"g","д"=>"d","Д"=>"d","е"=>"e","Е"=>"e","ё"=>"e","Ё"=>"e",
"ж"=>"zh","Ж"=>"zh","з"=>"z","З"=>"z","и"=>"i","И"=>"i","й"=>"j",
"Й"=>"j","к"=>"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","щ"=>"shh","Щ"=>"shh","ъ"=>"",
"Ъ"=>"","ы"=>"y","Ы"=>"y","ь"=>"","Ь"=>"","э"=>"je","Э"=>"je",
"ю"=>"ju","Ю"=>"ju","я"=>"ja","Я"=>"ja" );
$text = strtr($text, $trans);
return $text;
}
Спустя 17 минут, 49 секунд (9.03.2012 - 08:42) h234 написал(а):
Пример ошибки, точнее с чем она не справляется?
Спустя 22 минуты, 39 секунд (9.03.2012 - 09:04) gamleton написал(а):
Или например """""""""""" так ili-naprimer-\\\\\\-tak ролпо
Моя функция "такого" не выдержит moja-funkcija- akogo-ne-vyderzhit
Спустя 5 минут, 22 секунды (9.03.2012 - 09:10) Visman написал(а):
Достаточно
$text = preg_replace('%[\x00-\x1f]%', '', $text);
$text = preg_replace('%[@=\' ]+%', '-', $text);
$trans = array("а"=>"a","А"=>"a","б"=>"b","Б"=>"b","в"=>"v","В"=>"v","г"=>"g",
"Г"=>"g","д"=>"d","Д"=>"d","е"=>"e","Е"=>"e","ё"=>"e","Ё"=>"e",
"ж"=>"zh","Ж"=>"zh","з"=>"z","З"=>"z","и"=>"i","И"=>"i","й"=>"j",
"Й"=>"j","к"=>"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","щ"=>"shh","Щ"=>"shh","ъ"=>"",
"Ъ"=>"","ы"=>"y","Ы"=>"y","ь"=>"","Ь"=>"","э"=>"je","Э"=>"je",
"ю"=>"ju","Ю"=>"ju","я"=>"ja","Я"=>"ja" );
$text = strtr($text, $trans);
$text = preg_replace('%[^\w\.-]+%', '', $text);
Спустя 6 минут, 57 секунд (9.03.2012 - 09:17) gamleton написал(а):
Ваш скрипт работает, только ещё нужно дописать преобразование точек и _ в -
Спустя 8 минут, 3 секунды (9.03.2012 - 09:25) Visman написал(а):
$text = preg_replace('%[\x00-\x1f]%', '', $text);
$text = preg_replace('%[@=_\.\' ]+%', '-', $text);
$trans = array("а"=>"a","А"=>"a","б"=>"b","Б"=>"b","в"=>"v","В"=>"v","г"=>"g",
"Г"=>"g","д"=>"d","Д"=>"d","е"=>"e","Е"=>"e","ё"=>"e","Ё"=>"e",
"ж"=>"zh","Ж"=>"zh","з"=>"z","З"=>"z","и"=>"i","И"=>"i","й"=>"j",
"Й"=>"j","к"=>"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","щ"=>"shh","Щ"=>"shh","ъ"=>"",
"Ъ"=>"","ы"=>"y","Ы"=>"y","ь"=>"","Ь"=>"","э"=>"je","Э"=>"je",
"ю"=>"ju","Ю"=>"ju","я"=>"ja","Я"=>"ja" );
$text = strtr($text, $trans);
$text = preg_replace('%[^\w\-]+%', '', $text);
Спустя 7 минут, 11 секунд (9.03.2012 - 09:32) gamleton написал(а):
Благодарю
вы мне очень помогли +