Reset Bad Windows File Names

I found this function that checks if a string is a Windows file name and folder:

function is_valid_filename($name) { $parts=preg_split("/(\/|".preg_quote("\\").")/",$name); if (preg_match("/[az]:/i",$parts[0])) { unset($parts[0]); } foreach ($parts as $part) { print "part = '$part'<br>"; if (preg_match("/[".preg_quote("^|?*<\":>","/")."\a\b\c\e\x\v\s]/",$part)||preg_match("/^(PRN|CON|AUX|CLOCK$|NUL|COMd|LPTd)$/im",str_replace(".","\n",$part))) { return false; } } return true; } 

What I would prefer is a function that removes all the bad stuff from a string. I tried basically replacing preg_match with preg_replace, but not a cigar.

+7
php regex
source share
1 answer

Following the Gordon link , this gives:

 $bad = array_merge( array_map('chr', range(0,31)), array("<", ">", ":", '"', "/", "\\", "|", "?", "*")); $result = str_replace($bad, "", $filename); 
+17
source share

All Articles