【问题标题】:PHP Transliteration and renaming filesPHP音译和重命名文件
【发布时间】:2014-09-18 18:29:43
【问题描述】:

这是我的问题。文件没有重命名。我做错了什么?我没有看到什么?此脚本必须在 Windows 和 Unix 中工作。 UNIX UTF-8 不带 BOM 的脚本文件。尝试了 Windows 1251,ANSI,但仍然无法正常工作。

 <?php
 function Transliteration($FileName){ 
 $CharReplace = array (
'А'=>'A', 'Б'=>'B', 'В'=>'V',
'Г'=>'G', 'Д'=>'D', 'Е'=>'E',
'Ё'=>'E', 'Ж'=>'ZH', 'З'=>'Z',
'И'=>'I', 'Й'=>'J', 'К'=>'K',
'Л'=>'L', 'М'=>'M', 'Н'=>'N',
'О'=>'O', 'П'=>'P', 'Р'=>'R',
'С'=>'S', 'Т'=>'T', 'У'=>'U',
'Ф'=>'F', 'Х'=>'H', 'Ц'=>'TS',
'Ч'=>'CH', 'Ш'=>'SH', 'Щ'=>'SHH',
'Ъ'=>'', 'Ы'=>'I', 'Ь'=>'',
'Э'=>'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', 'х'=>'h', 'ц'=>'ts',
'ч'=>'ch',  'ш'=>'sh', 'щ'=>'shh',
'ъ'=>'', 'ы'=>'i', 'ь'=>'',
'э'=>'e', 'ю'=>'yu', 'я'=>'ya',
"№"=>"N", " "=>"_", "–"=>"_",
"-"=>"_", " - "=>"_", ","=>"");
$FileNameTranslited = str_replace(array_keys($CharReplace), $CharReplace, $FileName);
return $FileNameTranslited;}

function Renaming(){
$WorkDir = opendir("ToRename") or die("Не могу открыть папку");
while ($CurrentFile = readdir($WorkDir)){
    if ($CurrentFile != "." && $CurrentFile != ".."){
        $TranslitedFile = Transliteration($CurrentFile);
        if (rename($CurrentFile, $TranslitedFile))
            {echo "File Renamed";}
            else{echo "Some shit happen!";}
        echo $CurrentFile." -> ".$TranslitedFile."<br>";}}}

 Renaming();
 ?>

非常感谢 StathisG!这是解决问题的正确关键。但它仍然无法正常工作。看这里:

 function Renaming(){
 $directory = 'ToRename/';
 $WorkDir = opendir($directory) or die("Не могу открыть папку");
 while ($CurrentFile = readdir($WorkDir)){
   if ($CurrentFile != "." && $CurrentFile != ".."){
    $WhichCodingWeWant = 'UTF-8';
    $FileNameCoding = mb_detect_encoding($CurrentFile);
    echo $FileNameCoding."<br/>";
    $utf8_filename = mb_convert_encoding($CurrentFile, $WhichCodingWeWant, $FileNameCoding);
    $TranslitedFile = Transliteration($utf8_filename);
    mb_convert_encoding($TranslitedFile, $FileNameCoding, $WhichCodingWeWant);
    echo mb_detect_encoding($TranslitedFile)."<br/>";
    if (rename($directory . $CurrentFile, $directory . $TranslitedFile)) {
       echo "File Renamed<br/>";
       } else {
         echo "Some shit happen!<br/>";
          }
        echo $utf8_filename." -> ".$TranslitedFile."<br>";
       }
    }
 }
   Renaming(); 

如您所见,我添加了一个新变量“$WhichCodingWeWant”和“$FileNameCoding”。 传入文件名:“Новый текстовый документ.txt”出“Íîâûé_ГІГҐГЄГ±ГГîâûé_äîêóìåГГІov.txt”必须是“Novij_text” 我的脑袋炸了……


好的...步骤 3. 像以前一样的传入数据:Новый текстовый документ.txt
 function Renaming(){
 $directory = 'ToRename/';
 $WorkDir = opendir($directory) or die("Не могу открыть папку");
 while ($CurrentFile = readdir($WorkDir)){
    if ($CurrentFile != "." && $CurrentFile != ".."){
        echo "What name is come: ".$CurrentFile."<br/>";
        $WhichCodingWeWant = 'UTF-8';
        $FileNameCoding = mb_detect_encoding($CurrentFile);
        echo "File name encoding: ".$FileNameCoding."<br/>";

        $utf8_filename = mb_convert_encoding($CurrentFile, $WhichCodingWeWant, $FileNameCoding);
        echo "File name behind transliting: ".$utf8_filename."<br/>";
        $TranslitedFile = Transliteration($utf8_filename);
        echo "File name translited to: ".$TranslitedFile."<br/>";

        mb_convert_encoding($TranslitedFile, $FileNameCoding, $WhichCodingWeWant);
        echo "File name encoding converted to: ".mb_detect_encoding($TranslitedFile)."<br/>";

        if (rename($directory . $CurrentFile, $directory . $TranslitedFile)) {
            echo "File Renamed<br/>";
        } else {
            echo "Some shit happen!<br/>";
        }
        echo $utf8_filename." -> ".$TranslitedFile."<br>";
    }
 }
 }
 Renaming();

 Result is: 
 What name is come: Новый текстовый документ.txt
 File name encoding: UTF-8
 File name behind transliting: ????? ????????? ????????.txt
 File name translited to: ?????_?????????_????????.txt
 File name encoding converted to: ASCII

警告::第 32 行的 E:\WEB\XAMPP\htdocs\my\Site\test\test6.php 中没有错误 出事了! ??? ?????? ????????.txt -> ????????????????????????.txt 并且文件没有在文件夹中重命名。

如果我想要并制作 UTF-8,为什么要使用 ASCII?我明白了,我什么都不懂!无论如何谢谢你 StathisG 试图帮助我!明天我会在 Linux 系统中尝试这个脚本。并告诉你结果。如果您对这一切有一些想法,我会很高兴看到它:)

【问题讨论】:

  • 也许可以尝试使用multibyte string functions 之一。
  • 谢谢,但你能告诉我,我必须使用哪一个?我以前从没用过。
  • 我一开始以为是字符替换有问题,结果发现还有一个问题。看看我的回答。

标签: php file cross-platform renaming transliteration


【解决方案1】:

您的代码会产生以下警告:

警告: rename(test.txt, test.txt): 系统找不到文件 指定。

$CurrentFile 变量只保存文件名,而不是文件的完整路径。请尝试以下操作:

function Renaming(){
    $directory = 'ToRename/';
    $WorkDir = opendir($directory) or die("Не могу открыть папку");
    while ($CurrentFile = readdir($WorkDir)){
        if ($CurrentFile != "." && $CurrentFile != ".."){
            $utf8_filename = mb_convert_encoding($CurrentFile, 'UTF-8', 'GREEK');
            $TranslitedFile = Transliteration($utf8_filename);
            if (rename($directory . $CurrentFile, $directory . $TranslitedFile)) {
                echo "File Renamed";
            } else {
                echo "Some shit happen!";
            }
            echo $utf8_filename." -> ".$TranslitedFile."<br>";
        }
    }
}
Renaming();

我单独测试了您的 Transliteration 变量,它似乎工作正常(见下面的测试),所以请忽略我对多字节字符串函数的原始评论。

echo Transliteration('Не могу открыть папку'); // produces 'Ne_mogu_otkrit_papku'

编辑:

我编辑了上面的代码,添加了以下行:

$utf8_filename = mb_convert_encoding($CurrentFile, 'UTF-8', 'GREEK');

然后,我使用$utf8_filename 作为传递给Transliteration 函数的变量:

$TranslitedFile = Transliteration($utf8_filename);

您可能已经注意到,我使用“GREEK”作为文件名的编码,因为这是我知道的除英语之外的唯一语言,所以我使用希腊文件名来测试您的代码。

我创建了一个名为“τεστ.txt”的文件,并将以下值添加到$CharReplace 数组中:'τ'=&gt;'t', 'ε'=&gt;'e', 'σ'=&gt;'s'

当我运行代码时,我收到以下消息,并且文件已成功重命名为“test.txt”。

File Renamed τεστ.txt -> test.txt

根据 PHP 手册,mb_convert_encoding 支持的编码是 these

所以,试试上面的代码,用与你使用的字符对应的编码替换编码值,然后检查是否能解决你的问题。

【讨论】:

  • 是的,我以前试过这个。但这不起作用。例如:在“ToRename”文件夹中,我们有一个文件“Новый текстовый документ.txt”,在这个脚本之后我们必须有一个“Novij_textovij_document.txt”。但是我们没有!仅脚本后“Новый_текстовый_документ.txt”仅更改了空格。
  • 请不要像你的噩梦一样想我 :) 我编辑了我的问题。
  • @Baaakaaa Lol,没关系。 :) 不幸的是,我发现了很多关于西里尔字符的问题(我认为这是你需要的),至少对于运行 Windows 的系统没有解决方案。例如看看hereherehere
  • 另外,机器的语言环境也可能有一些事情要做。例如,我可以用希腊语写作,所以也许这就是希腊文件名的例子有效的原因。顺便说一句,如果你在尝试任何编码更改之前echo $CurrentFile;,你会得到什么?
  • 嗨 StathisG!查看帖子中的更新(第 3 步)并感谢您的链接。 Windows就像每次......没什么好。您使用的是哪个操作系统?你可以制作一个名为“Новый текстовый документ.txt”的文件进行测试吗?
猜你喜欢
  • 2014-09-09
  • 1970-01-01
  • 2014-01-20
  • 1970-01-01
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
相关资源
最近更新 更多