Remove invalid characters from a filename

You may also like...

12 Responses

  1. aaaaa says:

    thanks

  2. Richard says:

    Just what I needed, cheers!

  3. parveen sharma says:

    How do you remove characters such as “�” from this string, as well as the html code?

  4. Jawsper says:

    uhm… try this:

    <code>

    </code>

  5. hitendra says:

    hi friends, visit this new arena to code and other things. http://hktech.info

  6. riesurya says:

    That what I looking for since this morning. Thanks a lot 🙂

  7. Terenn says:

    preg_match('#[0-9a-z_]#', $temp[$i])

    would do the trick too

  8. Claudio says:

    I am trying to modify the search pattern to include only numbers, decimal separator (.) and aritmethic operators (+,-,*,/) with no success.

    Anybody can help me?

    Greetings from Paraguay.

    Claudio Bogado Pompa.

  9. Julien says:

    $fileName=preg_replace("/[^a-z0-9_]/i","",$fileName);

    would do the same (and faster: one line and preg is faster than str_replace)

    For files though you might want to allow more characters, such as:

    $fileName=preg_replace("/[^a-z0-9_-.]/i","",$fileName);

    In English: replace anything that's NOT a to z, 0 to 9, a ".", a "-" or a "_", case insensitive, with nothing.

  10. I noticed the code is incorrect:
    Should be : $filename = preg_replace("/[^a-z0-9_-s.]/i","",$filename);

    Also noticed that this works:
    $filename = preg_replace("/[^.a-z0-9_-]/i","",$filename); // not sure about this technique, please comment if breaks. top one seems best

  11. looking forward for more articles on your blog Cool to read, worth learning! Keep it up! http://www.canadagoose-paris.com/doudoune-canada-

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.