I use this little function a lot when dealing with taking a short string and making it 'clean' for things like filenames, or keywords and the like.
Helps strip out that garbage Œsc” too.
function Keep($orig_data='',$keepchars='',$casecontrol=''){
$keep_pattern = "/[^".$keepchars."]/".$casecontrol;
return preg_replace($keep_pattern,"",$orig_data);
}
You call it like:
$safe_string_for_filename = Keep($input,'a-z0-9_-','i');
It allwos you to custom throw in chars you want to "keep" (thus the name of the function). If you pass it the 'i' in the third param, then it wont care about case, otherwise it would (just a feature I used rarely). It will take ranges, a-z 0-9, and other specific chars (even a space too).
Works really well, and I have yet to run into a problem with it.
This function stemmed from my previous experience with a pathetic language called "Tango"... it had a nice "Keep" function that worked crudely like this.
ANYHOW, to your issue, just run your input string through the Keep function, and it will strip out any nasty chars that you do not want. And since you only want to use it for a filename, then the above example would work great, since it only keeps chars that are safe for a filename!