перевод текста в транслит и обратно в русский

Discussion in 'PHP' started by GAiN, 30 Nov 2013.

  1. GAiN

    GAiN Elder - Старейшина

    Joined:
    2 Apr 2011
    Messages:
    2,529
    Likes Received:
    166
    Reputations:
    99
    Всем привет

    написал такой код
    PHP:
    $str $_POST[russ_text'];
     
    function translit($str)
    {
    $tr = array(
    "А"=>"A","Б"=>"B","В"=>"V","Г"=>"G",
    "Д"=>"D","Е"=>"E","Ж"=>"J","З"=>"Z","И"=>"I",
    "Й"=>"Y","К"=>"K","Л"=>"L","М"=>"M","Н"=>"N",
    "О"=>"O","П"=>"P","Р"=>"R","С"=>"S","Т"=>"T",
    "У"=>"U","Ф"=>"F","Х"=>"H","Ц"=>"TS","Ч"=>"CH",
    "Ш"=>"SH","Щ"=>"SCH","Ъ"=>"","Ы"=>"YI","Ь"=>"",
    "Э"=>"E","Ю"=>"YU","Я"=>"YA","а"=>"a","б"=>"b",
    "в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"j",
    "з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l",
    "м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
    "с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
    "ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"y",
    "ы"=>"yi","ь"=>"'","
    э"=>"e","ю"=>"yu","я"=>"ya",
    "
    ."=>".","?"=>"?","/"=>"_","\\"=>"_",
    "
    *"=>"_",":"=>":","*"=>"_","\""=>"_","<"=>"_",
    ">"=>"_","|"=>"_"
    );
    return 
    strtr($str,$tr);
    };
    $tans=translit($str);
    echo 
    $tans;
    можно ли заставить работать перевод из транслита в обратное направление, что бы перевести опять в русский ?
     
    #1 GAiN, 30 Nov 2013
    Last edited: 30 Nov 2013
  2. fl47590

    fl47590 Member

    Joined:
    15 Nov 2013
    Messages:
    54
    Likes Received:
    8
    Reputations:
    0
    В том варианте, как у тебя, нет. Почему? Потому что не выполняется условие Фано:
    Т.е. существует неоднозначность перевода. Варианты решения проблемы: 1. Забить 2. Сделать код префиксным (почитай в статье "Префиксный код" на Вики).
     
    #2 fl47590, 1 Dec 2013
    Last edited: 1 Dec 2013
  3. попугай

    попугай Elder - Старейшина

    Joined:
    15 Jan 2008
    Messages:
    1,515
    Likes Received:
    400
    Reputations:
    196
    array_flip
     
  4. fl47590

    fl47590 Member

    Joined:
    15 Nov 2013
    Messages:
    54
    Likes Received:
    8
    Reputations:
    0
    У него проблема в отстутствие однозначности перевода translit->rus. Например, строку "SCH" можно понять как "СЧ" или как "Щ". О чем я ему и написал.
     
Loading...