删除目录中包含特殊字符的文件

I tried removing files in directory with special characters using php.

I used the unlink() function and it worked as it is, but when the file contains special chars, it can't locate the file.

Now I use str_replace() function to replace the name.

See example below.

<?php
   //replace "+" sign with space.
   $filename = str_replace("+", " ", $filename); 

   $dir = "_resources/docs/";
   unlink($dir . $filename);
?>

It works but how about files with a name like this? [Vouching_Sample_02]-SMCC_Q3_Vouching_September_Goods_012216.

Is there any good recommendations for this?

Try to use this kind of solution.

$name = "This-is_a__test--(name)";
$find = array("-", "_", "--", "(", ")");
$replace = array(" ", " ", " ", " ", " ");
$converted = str_replace($find, $replace, $name);

echo $converted;

Out put: This is a test name

Include what you want to search in fine array.

Include what you want to replace with in replace array.

Else use preg_replace() with regular expression