0

我尝试替换一个包含特殊字符的文件

从:

[v]_CØDE:BREAKER_-_01_[gg][D00B1].mkv

至:

[v]_CoDE:BREAKER_-_01_[gg][D00B1].mkv

但是当我尝试下面的代码时

if ($handle = opendir($refolder)) {
  while (false !== ($entry = readdir($handle))) {
    $renameTHEfile = $entry;
    $renameTHEfile = str_ireplace("Ø","O",$renameTHEfile);
    $renameTHEfile = str_ireplace('ø','o',$renameTHEfile);
    $renameTHEfile = str_ireplace(":","_",$renameTHEfile);
    $renameTHEfile = str_ireplace("!","",$renameTHEfile);
    $renameTHEfile = str_ireplace("'","",$renameTHEfile);
    $renameTHEfile = str_ireplace('"','',$renameTHEfile);
    $renameTHEfile = str_ireplace(",","",$renameTHEfile);
    $renameTHEfile = str_ireplace("#","",$renameTHEfile);
    $renameTHEfile = str_ireplace("$","",$renameTHEfile);
    $renameTHEfile = str_ireplace('/','',$renameTHEfile);
    $renameTHEfile = str_ireplace("?","",$renameTHEfile);


    $theoldfile_rename = $refolder . $entry;

    $thenewfile_rename = $refolder . $renameTHEfile;


    $cmd_renameit = 'move "' .$theoldfile_rename. '" "'.$thenewfile_rename .'"';

    echo $cmd_renameit;

    exec($cmd_renameit);
  }
}

视频文件将输出为

[v]_CØDE

如何将其正确替换为,我还需要将 : 替换为 _

[v]_CODE_BREAKER_-_01_[gg][D00B1].mkv

感谢您的帮助。

4

2 回答 2

1

只需创建一个根据您的需要重命名的函数,您可以考虑这样的事情:

<?php
function rename_ani($name){
    $name = str_replace(array("Ø","ø"), "O", $name);
    $name = str_replace(":","_",$name);
    $name = str_replace(array("!","'",'"',",","#","$","/","?"),"",$name);
    return $name;
}

$anime = '[v]_CØDE:BREAKER_-_01_[gg][D00B1].mkv';
echo rename_ani($anime); // output: [v]_CODE_BREAKER_-_01_[gg][D00B1].mkv

?>
于 2012-10-07T09:58:33.457 回答
0

您在文件中有一个空格。将其转义为:“\”或其他字符。在你的情况下,只需删除空间。str_replace(" ", "", $str)

于 2012-10-07T05:58:57.570 回答