Skip to content

Instantly share code, notes, and snippets.

@esvit
Created May 16, 2015 05:54
Show Gist options
  • Save esvit/0a7f1769c1ac56405c50 to your computer and use it in GitHub Desktop.
Save esvit/0a7f1769c1ac56405c50 to your computer and use it in GitHub Desktop.
Recursive rename encoded cyrillic files (like #U0411...) to UTF-8 (wordpress problem)
<?php
function utf8_urldecode($str) {
$str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str));
$str = html_entity_decode($str,null,'UTF-8');
//$str = iconv('UTF-8', 'windows-1251', $str);
return $str;
}
$str = utf8_urldecode($str);
$directory = new RecursiveDirectoryIterator(__DIR__);
$flattened = new RecursiveIteratorIterator($directory);
$files = new RegexIterator($flattened, '#^.*(\#U\d\d\d\d[^/]+)\.*#i');
foreach($files as $file) {
$newFileName = utf8_urldecode(str_replace('#', '%', $file));
rename($file, $newFileName);
echo $file . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment