簡體   English   中英

PHP音譯和重命名文件

[英]PHP Transliteration and renaming files

這是我的問題。 文件未重命名。 我做錯了什么? 我看不到什么? 該腳本必須在Windows和Unix中工作。 UNIX UTF-8 w / o 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(); 

如您所見,我添加了一個新的Vars“ $ WhichCodingWeWant”和“ $ FileNameCoding”。 傳入文件名:“Новыйтекстовыйдокумент.txt”在“Íîâûé_òåêññîîâûГ__äîêóГГГГГГГ.І.txt”中輸入“ Novij_textovij_document.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 

警告::第32行的E:\\ WEB \\ XAMPP \\ htdocs \\ my \\ Site \\ test \\ test6.php中沒有錯誤。 ????? ????????? ?????????。txt-> ????? ????????? ?????????。txt文件未在文件夾中重命名。

如果要使用UTF-8,為什么要使用ASCII? 我了解我一無所知! 無論如何,謝謝StathisG試圖幫助我! 我明天將在Linux系統中嘗試該腳本。 並告訴您結果。 如果您對此有一些想法,我將很高興看到它:)

您的代碼產生以下警告:

警告:重命名(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數組中: 'τ'=>'t', 'ε'=>'e', 'σ'=>'s'

運行代碼時,收到以下消息,並且文件已成功重命名為“ test.txt”。

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

根據PHP手冊, mb_convert_encoding支持的編碼是這些

因此,請嘗試上面的代碼,將編碼值替換為與您使用的字符相對應的編碼,然后檢查是否可以解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM