F
F
Fyodor2014-12-16 10:14:40
PHP
Fyodor, 2014-12-16 10:14:40

How to translate the title of a material from Russian into a compatible transliteration url?

In general, the base of resort countries and each should have its own address.
Accordingly, from the name you need to translate Russian into a URL form.
the CMS I use has a built-in function, but it does not work correctly, especially if there are special characters in the string

for example - Ras al-Khaimah - does like - hajma Abu
Dhabi / Al Ain - dabi
More or less of this type only (ate up to two characters):

Of course, you can dig into the functions and find out where there is something wrong, but it is voluminous and many things do not fully understand what they are doing. Well, I think that for sure there is a ready-working-debugged function? How to find?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
itcoder, 2014-12-16
@Richard_Ferlow

Here is a working version:
UrlTranslitBehavior::makeUrlLink('Ras Al Khaimah');

<?php
class UrlTranslitBehavior{

  
  protected static function u_strtr ($value, $to, $from = null)
  {
    if (is_null ($from))
    {
      arsort ($to, SORT_LOCALE_STRING);
      foreach ($to as $c => $r)
      {
        $value = str_replace ($c, $r, $value);
      }
    }
    else
    {
      $len = min (strlen ($to), strlen ($from));
      for ($i = 0; $i < $len; ++$i)
      {
        $value = str_replace (
          mb_substr ($to, $i, 1, 'UTF-8'),
          mb_substr ($from, $i, 1, 'UTF-8'), 
          $value
        );
      }
    }
    return $value;
  }
  

  public static function translit ($value)
  {
    $value = trim ($value);
    
    $value = str_replace (
      array ("\r", "\n", "\t", ',', '.', '(', ')', '[', ']', '{', '}'),
      '',
      $value
    
    );
    
    if (!isset ($lang))
    {
      $regexpRus = '/^[а-яА-Я]+/';
      $lang = preg_match ($regexpRus, $value) ? 'ru' : 'en';
    }
      // Сначала заменяем "односимвольные" фонемы.
      $value = self::u_strtr ($value, "абвгдеёзийклмнопрстуфхыэ ", "abvgdeeziyklmnoprstufhie-");
      $value = self::u_strtr ($value, "АБВГДЕЁЗИЙКЛМНОПРСТУФХЫЭ ", "ABVGDEEZIYKLMNOPRSTUFHIE-");

      // Затем - "многосимвольные".
      $value = self::u_strtr (
        $value,
        array (
          "ж"=>"zh", "ц"=>"ts", "ч"=>"ch", "ш"=>"sh",
          "щ"=>"shch","ь"=>"", "ъ"=>"", "ю"=>"yu", "я"=>"ya",
          "Ж"=>"ZH", "Ц"=>"TS", "Ч"=>"CH", "Ш"=>"SH",
          "Щ"=>"SHCH","Ь"=>"", "Ъ"=>"", "Ю"=>"YU", "Я"=>"YA",
          "ї"=>"i", "Ї"=>"Yi", "є"=>"ie", "Є"=>"Ye",
          "&nbsp;"=>"-"
        )
      );
      
    return $value;
  }
  

  public static function makeUrlLink ($value)
  {
       	        $link = self::translit ($value, 'en');
    $link = preg_replace ('/([^0-9a-zA-Z_])+/', '-', $link);
    $link = preg_replace ('/[_]{2,}/', '_', $link);
    $link = preg_replace ('/^_/', '', $link);
    $link = preg_replace ('/_$/', '', $link);
    $link = trim($link,'-');
    return strtolower($link);
  }
  
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question