If you're certain that you only want to keep a-z, 0-9, and period, you can do the following:
$filename = preg_replace ("/[^0-9a-z\.]/i", "_", $filename);
Basically, it will replace all that is not (denoted by ^ at the beginning of [ ... ] block) number, letter, and period.
The 'i' modifier tells PHP to treat the string as case insensitive.
And I noticed that you also want to do a check for file extension. So, here's some code:
<?php
$filename = "abcde$#!#%+^**)23_.4.jpg";
print "<p>original filename is: {$filename}</p>";
$filename = preg_replace ("/[^_0-9a-z\.]/i", "_", $filename);
print "<p>final filename is: {$filename}</p>";
# Get the file extension:
if (strpos ($filename, '.') !== false)
{
# Get the string after the last 'period'.
$extension = preg_replace ("/^.*?\.([^\.]*)$/", "$1", $filename);
}
else
{
$extension = '';
}
$extension = trim ($extension);
print "<p>file extension is: '{$extension}'</p>";
?>