Скрипт для массового переименования файлов с кириллицы на латиницу (транслитерации)

21.01.2020 19:56:50

Небольшая утилита на php позволяющая переименовать все директории и файлы в них с "русского" на "английский".

Внимание! Директории и файлы в них будут переименованы, т.ч. если Вам нужны старые имена, то сначала стоит сделать резервную копию.

renamer.php (Download)

 <?php
 
  class soReNamer
  {
    protected $_aResult = array();

    /**
    * toLatin
    * 
    * @param string $str
    * 
    * @return string
    */
    protected function _toLatin($str) {
        //$str = iconv('windows-1251', 'utf-8', $str);  
        $str = strtolower($str);

        $str = str_replace("&nbsp;", "", $str);
        $str = str_replace("/", "-", $str);
        $str = str_replace("\\", "", $str);
        $str = str_replace("(", "", $str);
        $str = str_replace(")", "", $str);
        $str = str_replace(":", "", $str);
        $str = str_replace(" ", "_", $str);
        $str = str_replace("!", "", $str);
        $str = str_replace("|", "_", $str);
        $str = str_replace(".", "_", $str);
        $str = str_replace("№", "N", $str);
        $str = str_replace("?", "", $str);
        $str = str_replace("&nbsp", "_", $str);
        $str = str_replace("&amp;", '_', $str);
        $str = str_replace("ь", "", $str);
        $str = str_replace("Ь", "", $str);
        $str = str_replace("ъ", "", $str);
        $str = str_replace("«", "", $str);
        $str = str_replace("»", "", $str);
        $str = str_replace("“", "", $str);
        $str = str_replace(",", "", $str);
        $str = str_replace("™", "", $str);
        $str = str_replace("’", "", $str);
        $str = str_replace("®", "", $str);
        $str = str_replace(array('&#43;', '&#43'), '+', $str);

        $new_str = '';
        $_Array = array(" " => "_", "а" => "a", "б" => "b", "в" => "v", "г" => "g", "д" => "d", "е" => "e", "ё" => "e", "ж" => "zh", "з" => "z", "и" => "i", "й" => "y", "к" => "k", "л" => "l", "м" => "m", "н" => "n", "о" => "o", "п" => "p", "р" => "r", "с" => "s", "т" => "t", "у" => "u", "ф" => "f", "х" => "h", "ц" => "c", "ч" => "ch", "ш" => "sh", "щ" => "sch", "ъ" => "i", "ы" => "y", "ь" => "i", "э" => "e", "ю" => "u", "я" => "ya", "А" => "a", "Б" => "b", "В" => "v", "Г" => "g", "Д" => "d", "Е" => "e", "Ё" => "e", "Ж" => "zh", "З" => "z", "И" => "i", "Й" => "y", "К" => "k", "Л" => "l", "М" => "m", "Н" => "n", "О" => "o", "П" => "p", "Р" => "r", "С" => "s", "Т" => "t", "Ы" => "Y", "У" => "u", "Ф" => "f", "Х" => "h", "Ц" => "c", "Ч" => "ch", "Ш" => "sh", "Щ" => "sch", "Э" => "e", "Ю" => "u", "Я" => "ya", "." => "_", "$" => "i", "%" => "i", "&" => "_and_");

        $chars = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);

        foreach ($chars as $val)
            if (empty($_Array[$val]))
                $new_str.=$val;
            else
                $new_str.=$_Array[$val];
        return preg_replace('([^a-z0-9/_\.-])', '', $new_str);
    }


    protected function _makeName($sFullPath)
    {
      $sName = substr($sFullPath, strripos($sFullPath, '/') + 1);
      $sNewName = $this->_toLatin($sName);
      $sNewName = str_replace('_jpg', '.jpg', $sNewName);
      return str_replace($sName, $sNewName, $sFullPath);
    }
    
    /**
    * Scan Path
    * 
    * @param string $sPath
    * 
    * @return void
    */
    protected function _scan($sPath) 
   {
      $d = dir($sPath);
      while (false !== ($entry = $d->read()))
      {
        if ($entry != '.' && $entry != '..')
        {
          $sName = $sPath.'/'.$entry;
          if (is_dir($sName))
          {
            $sNewName = $this->_makeName($sName);
            rename($sName, $sNewName);
            $this->_scan($sNewName);
          }
          else
          {
//            $this->_aResult[] = $this->_makeName($sName);
            rename($sName, $this->_makeName($sName));
          }
        }
      }
      $d->close();
    }
     
    /**
    * Main
    * 
    * @param string $sPath
    * 
    * @return array
    */
    public function scan($sPath) 
    {
       $this->_scan($sPath);
       return $this->_aResult;
    }
  }
 
  
  $oScaner = new soReNamer();
  echo '<pre>';
    print_r($oScaner->scan(__DIR__.'/images_in/все работы'));
  echo '</pre>';



Категории: PHP, Script / Tool
Яндекс.Метрика