简体   繁体   English

PHP音译和重命名文件

[英]PHP Transliteration and renaming files

This is my problem. 这是我的问题。 Files is not renaming. 文件未重命名。 What I do wrong? 我做错了什么? What I do not see? 我看不到什么? This script is must to work in Windows and Unix. 该脚本必须在Windows和Unix中工作。 Script file in UNIX UTF-8 w/o BOM. UNIX UTF-8 w / o BOM中的脚本文件。 Tryed Windows 1251, ANSI and still don't working. 尝试使用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();
 ?>

Thanks a lot StathisG! 非常感谢StathisG! This is a right key for solution. 这是解决方案的正确方法。 But it still not working. 但它仍然无法正常工作。 Look here: 看这里:

 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(); 

As you can see I add a new Vars "$WhichCodingWeWant" and "$FileNameCoding". 如您所见,我添加了一个新的Vars“ $ WhichCodingWeWant”和“ $ FileNameCoding”。 Incoming file name: "Новый текстовый документ.txt" out "Íîâûé_òåêñòîâûé_äîêóìåГГІ.txt" mus be "Novij_textovij_document.txt" My brain is exploded... 传入文件名:“Новыйтекстовыйдокумент.txt”在“Íîâûé_òåêññîîâûГ__äîêóГГГГГГГ.І.txt”中输入“ Novij_textovij_document.txt”。 。


Okay...step 3. Incoming data like before: Новый текстовый документ.txt 好的...第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 

Warning: : No error in E:\\WEB\\XAMPP\\htdocs\\my\\Site\\test\\test6.php on line 32 Some shit happen! 警告::第32行的E:\\ WEB \\ XAMPP \\ htdocs \\ my \\ Site \\ test \\ test6.php中没有错误。 ????? ????? ????????? ????????? ????????.txt -> ????? ?????????。txt-> ????? ????????? ????????? ????????.txt And file is not renamed in folder. ?????????。txt文件未在文件夹中重命名。

Why ASCII if I want and making UTF-8? 如果要使用UTF-8,为什么要使用ASCII? I understand that a I nothing to understand! 我了解我一无所知! Any way Thank You StathisG for trying to help me! 无论如何,谢谢StathisG试图帮助我! I'll try this script tomorrow in Linux system. 我明天将在Linux系统中尝试该脚本。 And tell you about results. 并告诉您结果。 If you will have a some ideas about this all, I will glad to see it :) 如果您对此有一些想法,我将很高兴看到它:)

Your code produces the following warning: 您的代码产生以下警告:

Warning: rename(test.txt, test.txt): The system cannot find the file specified. 警告:重命名(test.txt,test.txt):系统找不到指定的文件。

The $CurrentFile variable holds only the filename and not the complete path of the file. $CurrentFile变量仅保存文件名,而不保存文件的完整路径。 Try the following: 请尝试以下操作:

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();

I tested your Transliteration variable separately, and it seems to working fine (see test below), so ignore my original comment about the multibyte string functions. 我分别测试了您的Transliteration变量,它似乎运行良好(请参见下面的测试),所以请忽略我有关多字节字符串函数的原始注释。

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

EDIT: 编辑:

I edited the code above, adding the following line: 我编辑了上面的代码,添加了以下行:

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

Then, I used the $utf8_filename as the variable passed to your Transliteration function: 然后,我将$utf8_filename用作传递给您的Transliteration功能的变量:

$TranslitedFile = Transliteration($utf8_filename);

As you may noticed, I used 'GREEK' as the filename's encoding, since that's the only language I know other than English, so I used Greek filenames to test your code. 您可能会注意到,我使用“ GREEK”作为文件名的编码,因为这是除英语之外我唯一知道的语言,因此我使用希腊文件名来测试您的代码。

I created a file called "τεστ.txt", and added the following values to the $CharReplace array: 'τ'=>'t', 'ε'=>'e', 'σ'=>'s' 我创建了一个名为“τεστ.txt”的文件,并将以下值添加到$CharReplace数组中: 'τ'=>'t', 'ε'=>'e', 'σ'=>'s'

When I run the code, I got the following message, and the file was renamed successfully to "test.txt". 运行代码时,收到以下消息,并且文件已成功重命名为“ test.txt”。

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

Based on the PHP manual, the supported encodings for mb_convert_encoding are these . 根据PHP手册, mb_convert_encoding支持的编码是这些

So, try the above code, replacing the encoding value with the encoding which corresponds to the characters you use and check if that solves your problem. 因此,请尝试上面的代码,将编码值替换为与您使用的字符相对应的编码,然后检查是否可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM