我发现这个函数测试字符串是否是
Windows文件名和文件夹友好:
function is_valid_filename($name) { $parts=preg_split("/(\/|".preg_quote("\\").")/",$name); if (preg_match("/[a-z]:/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; }
我宁愿拥有的是一个从字符串中删除所有不良内容的函数.我试着基本上用preg_replace替换preg_match但没有雪茄.
继Gordon的
reference之后,这给出了:
$bad = array_merge( array_map('chr',range(0,31)),array("<",">",":",'"',"/","\\","|","?","*")); $result = str_replace($bad,"",$filename);