ravix76 Posted December 29, 2010 Share Posted December 29, 2010 I have a user inputted string which I which to refine for use as a filename. The string might include special characters such as £ % etc which I want to strip out or substitute. I'll then convert to lowercases, append a file extension and use it for the uploaded file / image. I having difficulty making the string safe for filename use. I essentially want to strip out any non a-z or 0-9 characters and strip out any spaces (or substitute with a dash). So "CrazyDave's Upload!" would become "crazydaves-upload.jpg" Do I need regular expressions? <-- seems like a deep topic! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/222901-refine-string-for-filename/ Share on other sites More sharing options...
PHPTOM Posted December 29, 2010 Share Posted December 29, 2010 <?PHP function validFileName($text, $extension = 'jpg'){ return ereg_replace("[^A-Za-z0-9\-]", "", str_replace(' ', '-', strtolower(trim($text)))).'.'.$extension; } echo validFileName("CrazyDave's Upload!"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/222901-refine-string-for-filename/#findComment-1152538 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.