You might try to use preg_replace()
for this:
<?php
header('Content-Type: text/plain');
$file = '!@#$%^&*()ashdgf$%^&*(.pdf';
$file = preg_replace('/[^a-z\.\-\_]/i', '_', $file);
var_dump($file);
?>
Shows:
string(26) "__________ashdgf______.pdf"
If \
and ^
are acceptable charaters, then use this:
<?php
header('Content-Type: text/plain');
$file = 'folder\as!@#$%^&*()ashdgf$%^&*(.zip';
$file = preg_replace('/[^a-z\.\-\_\\\\^]/i', '_', $file);
var_dump($file);
?>
Shows:
string(35) "folder\as_____^____ashdgf__^___.zip"
You may add any other escaped symbols from whitelist to this expression.