Prevent foreign characters in filenames? It seems that some of our users are uploading filenames with foreign characters...



Is there any way to prevent this?

I'd like to keep the filenames to all English letters (plus numbers, dash &amp; underscore).

(If relevant, this is for Craft 2.)
Cant you just use preg_replace("/[^A-Za-z0-9 ]/", '', $stuff) for this? Seems that will strip away anything that isnt a number or a letter from the english alphabet?

EDIT ---------
Challenge accepted ;)

public function modifyAssetFilename($filename){
    return preg_replace("/[^A-Za-z0-9-]/", '', $filename);
}


Something according to the above strips out all unwanted characters using the hook if you place it in a plugins main class file. The above preg_replace should also keep dashes e.t.c.

Another way you might be able to approach the sanitation of filenames is listening to the onBeforeSaveAsset method and then using craft()-&gt;assets-&gt;renameFile() once you have sanitized it. Not sure which one is better. Both should work.