Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Плагин для транслитерации русских имен файлов загружаемых в ModX Revolution
/*Плагин для транслитерации русских имен файлов загружаемых в ModX Revolution через менеджер файлов. Все русские символы переводятся
в нижний регистр и транслителируются, спецсимволы заменяются на "_". Привязывать к событию OnFileManagerUpload. */
<?php
function UrlTranslit($string) {
$alfavitlover = explode(',', 'ё,й,ц,у,к,е,н,г,ш,щ,з,х,ъ,ф,ы,в,а,п,р,о,л,д,ж,э,я,ч,с,м,и,т,ь,б,ю');
$alfavitupper = explode(',', 'Ё,Й,Ц,У,К,Е,Н,Г,Ш,Щ,З,Х,Ъ,Ф,Ы,В,А,П,Р,О,Л,Д,Ж,Э,Я,Ч,С,М,И,Т,Ь,Б,Ю');
for ($wr = 0; $wr < sizeof($alfavitupper); $wr++) {
$string = str_replace($alfavitupper[$wr], $alfavitlover[$wr], $string);
}
$LettersFrom = explode(",", "ё,й,у,к,е,н,г,з,х,ф,ы,в,а,п,р,о,л,д,э,с,м,и,т,б");
$LettersTo = explode(',', 'e,j,u,k,e,n,g,z,x,f,y,v,a,p,r,o,l,d,e,s,m,i,t,b');
static $Consonant = "бвгджзйклмнпрстфхцчшщ";
static $Vowel = "аеёиоуыэюя";
static $BiLetters = array (
"ж" => "zh",
"ц" => "ts",
"ч" => "ch",
"ш" => "sh",
"щ" => "sch",
"ю" => "ju",
"я" => "ja",
);
//here we replace ъ/ь
$string = preg_replace("/(ь|ъ)([" . $Vowel . "])/", "j\\2", $string);
$string = preg_replace("/(ь|ъ)/", "", $string);
$string = str_replace($LettersFrom, $LettersTo, $string);
// $string = strtr($string, $LettersFrom, $LettersTo );
$string = strtr($string, $BiLetters);
$string = preg_replace("/j{2,}/", "j", $string);
//$string = preg_replace("/[^a-z0-9_]/", "_", $string);
$string = preg_replace("/[^0-9a-z_\-\.]/", "", $string);
return $string;
}
foreach ($files as $file) {
$ext = @pathinfo($file['name'],PATHINFO_EXTENSION);
rename($directory->getPath().$file['name'], $directory->getPath().UrlTranslit($file['name']));
}
@geforester

This comment has been minimized.

Copy link

commented May 8, 2012

В версии 2.2.2 отказывается работать.

@govza

This comment has been minimized.

Copy link

commented Jun 30, 2012

$currentdoc = $modx->newObject('modResource');
foreach($files as $file)
{
    if($file['error'] == 0)
    {
      $pathInfo = pathinfo($file['name']);
      $oldPath = $directory . $file['name'];
      $newPath = $currentdoc->cleanAlias($pathInfo['filename']).'-'.date("Y-m-d-h-s").'.'. $pathInfo['extension'];

      $source->renameObject($oldPath, $newPath);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.