<p>Cant you just use <code>preg_replace("/[^A-Za-z0-9 ]/", '', $stuff)</code> for this? Seems that will strip away anything that isnt a number or a letter from the english alphabet?</p>

<p>EDIT ---------
Challenge accepted ;)</p>

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

<p>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.</p>

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

CC BY-SA 3.0
2018-03-21T19:41:59.200Z
Based on my own research, looks like you're halfway there. If your answer mentioned how to use this in the context of the `modifyAssetFilename` hook, I would mark it as correct.
Lindsey D
CC BY-SA 3.0
2018-03-21T20:06:55.507Z
Excellent, thank you! I also appreciate the alternative option.
Lindsey D
CC BY-SA 3.0
2018-03-21T19:38:32.247Z
2018-03-21T19:58:27.403Z
2018-03-21T19:58:27.403Z
Gįëł Tėttëłæår
<p>It seems that some of our users are uploading filenames with foreign characters...</p>

<p><a href="https://i.sstatic.net/Q4dBF.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Q4dBF.png" alt="uploaded files"></a></p>

<p><strong>Is there any way to prevent this?</strong></p>

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

<p>(If relevant, this is for Craft 2.)</p>

CC BY-SA 3.0
2018-03-21T18:36:02.307Z
I'm not sure how you'd intelligently map non-roman characters to roman characters. Is there a reason you need to exclude foreign characters?
andrew.welch
CC BY-SA 3.0
2018-03-21T18:44:11.913Z
Long story... these images get pushed out in a weekly email. Then that email gets forwarded via a GroupMail process (which I am not familiar with). The foreign characters seem to be breaking GroupMail.
Lindsey D
CC BY-SA 3.0
2018-03-21T18:44:54.383Z
Honestly, even just stripping them out would work. The actual filenames are unimportant, so even replacing each one with a timestamp might be sufficient.
Lindsey D
<assets><upload><files>
Prevent foreign characters in filenames?
# Prevent foreign characters in filenames?

(Asked by: username_0 on 2018-03-21)

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

[![uploaded files](https://i.sstatic.net/Q4dBF.png)](https://i.sstatic.net/Q4dBF.png)

**Is there any way to prevent this?**

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

(If relevant, this is for Craft 2.)

**Tags:** assets, upload, files
> **Comments:**
> **username_1**: I'm not sure how you'd intelligently map non-roman characters to roman characters. Is there a reason you need to exclude foreign characters?  
> **username_0**: Long story... these images get pushed out in a weekly email. Then that email gets forwarded via a GroupMail process (which I am not familiar with). The foreign characters seem to be breaking GroupMail.  
> **username_0**: Honestly, even just stripping them out would work. The actual filenames are unimportant, so even replacing each one with a timestamp might be sufficient.  

---

## 1 answers

### Answer by username_2 on 2018-03-21. Score: 2 | Accepted answer

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()->assets->renameFile() once you have sanitized it. Not sure which one is better. Both should work.

> **Comments:**
> **username_0**: Based on my own research, looks like you're halfway there. If your answer mentioned how to use this in the context of the `modifyAssetFilename` hook, I would mark it as correct.  
> **username_0**: Excellent, thank you! I also appreciate the alternative option.  

---

