PHP str_replace

时间:2012-10-07 05:38:56

标签: php

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

自:

[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

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

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

感谢您的帮助。

2 个答案:

答案 0 :(得分: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

?>

答案 1 :(得分:0)

文件中有空格。逃避它:“\”或其他一些角色。在您的情况下,只需删除空格。 str_replace(“”,“”,$ str)