V
V
vadikjust2020-05-17 11:36:11
PHP
vadikjust, 2020-05-17 11:36:11

How to transliterate in both directions?

There is a transliteration function, but the problem is that if there is a b in the string, the b (soft and hard sign) is replaced in the transliteration by ' and '', which does not suit me, because I pass the string to the url and pull out the string from this url and back I translate into Russian.

What characters can replace the hard and soft signs so that there are no quotes and the string is changed to Russian in its original state?

function TranslitURL ($text, $translits = 'ru_en') { 
$translit = array(
   
            'а' => 'a',   'б' => 'b',   'в' => 'v',
  
            'г' => 'g',   'д' => 'd',   'е' => 'e',
  
            'ё' => 'yo',   'ж' => 'zh',  'з' => 'z',
  
            'и' => 'i',   'й' => 'j',   'к' => 'k',
  
            'л' => 'l',   'м' => 'm',   'н' => 'n',
  
            'о' => 'o',   'п' => 'p',   'р' => 'r',
  
            'с' => 's',   'т' => 't',   'у' => 'u',
  
            'ф' => 'f',   'х' => 'x',   'ц' => 'c',
  
            'ч' => 'ch',  'ш' => 'sh',  'щ' => 'shh',
  
            'ь' => '\'',  'ы' => 'y',   'ъ' => '\'\'',
  
            'э' => 'e',   'ю' => 'yu',  'я' => 'ya',
          
  
            'А' => 'A',   'Б' => 'B',   'В' => 'V',
  
            'Г' => 'G',   'Д' => 'D',   'Е' => 'E',
  
            'Ё' => 'YO',   'Ж' => 'Zh',  'З' => 'Z',
  
            'И' => 'I',   'Й' => 'J',   'К' => 'K',
  
            'Л' => 'L',   'М' => 'M',   'Н' => 'N',
  
            'О' => 'O',   'П' => 'P',   'Р' => 'R',
  
            'С' => 'S',   'Т' => 'T',   'У' => 'U',
  
            'Ф' => 'F',   'Х' => 'X',   'Ц' => 'C',
  
            'Ч' => 'CH',  'Ш' => 'SH',  'Щ' => 'SHH',
  
            'Ь' => '\'',  'Ы' => 'Y\'',   'Ъ' => '\'\'',
  
            'Э' => 'E',   'Ю' => 'YU',  'Я' => 'YA',
  
        );
  
       
    
  if($translits == 'en_ru') { 
 $word = strtr($text, array_flip($translit)); 
 $word = str_replace( "_", " ", $word);
    } else {
$word = strtr($text, $translit);
$word = str_replace( " ", "_", $word);
    }
  return $word; 
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
FanatPHP, 2020-05-17
@FanatPHP

Learn to google your question title

G
granty, 2020-05-17
@granty

if there is a ъ (soft and hard sign) in the string, it is replaced in transliteration with ' and '', which does not suit me, because I pass the string to the url and already from this url I pull out the string and translate it back into Russian
Why? According to RFC3986 , all characters that are not allowed in a URL must be encoded, and PHP provides the rawurlencode()and functions for this rawurldecode().
Just process the url with them, they will correctly encode your ' and then decode them back (the only thing left to do is to escape them when you shove them into the SQL query).
How do you get urls like https://ru.wikipedia.org/wiki/CharacterSet ?
But your URLs will be made exactly in accordance with GOST 7.79-2000 "RULES FOR TRANSLITERATION OF THE KIRILLOV LETTER IN THE LATIN ALPHABET"
( KIRILLOVSKY , damn it, non-Russian devils!)
why do you need a cnc?
Но что-то мне подсказывает, что ЧПУ на сайте вам нужно совсем для другого...
Только вы не поставили тег про "это другое" в своём вопросе. Поэтому, даже решив проблему с кодированием Ь и Ъ, вы получите неправильное ЧПУ с точки зрения этого "чего-то другого" - того, о чём вы даже и не спросили.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question