5 7 Votes

PHP: Remove invalid characters from file names

Tip by Axuter | 23/01/2013 at 22:02

Characters such as "?", "\", "/", "*" or "|" are not allowed in file names on the Windows operating system. With this line of code, you can simply delete those characters from a string with the help of PHP:

$f = 'fi?le.txt';

$f = str_replace(array('\\','/',':','*','?','"','<','>','|'),' ',$f);

echo $f; // 'fi le.txt'

The respective characters are simply replaced by a space.

Alternatively, you can also just use another character instead of the space or write '' to the replace function in order to delete the characters completely.


About the Author

Avatar AuthorThe author has not added a short description to his profile yet.
Show Profile | Message


Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own articles on askingbox.com. How to do.