Remove space and special character from string

I use this code to save the downloaded file, but the problem is that it has a file with a name with a special character and a space. for example allows

hi how are you 

but I do not want to allow any space, special charater, etc. Here is my .i code tried with preg_replace in uri, but after that I tried to download the file, but nothing was loaded.

 function save_file($file) { $allowed_ext = array('jpg','png','gif','jpeg'); $ext = $file['name']; $ext = strtolower($ext); if (in_array($ext, $allowed_ext)) { die('Sorry, the file type is incorrect:'.$file['name']); } $fname = date("H_i",time()).'_'.get_rand(5); $dir = date("Ym",time()); $folder = 'uploads/userfiles/'.$dir; $uri = $folder.'/'.$fname.'.'.$ext; if (!is_dir($folder)) mkdir($folder, 0777); if (copy($file['tmp_name'],$uri)) return $uri; else { return false; } } 
+4
source share
1 answer

To cut letters from a string, you can use the following regular expression

 $input = preg_replace("/[^a-zA-Z]+/", "", $input); 
+17
source

All Articles