简体   繁体   English

剥去坏的Windows文件名字符

[英]Strip bad Windows filename characters

I found this function that tests whether a string is Windows filename and folder friendly: 我发现这个函数测试一个字符串是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;
 }

What I'd rather have is a function that strips all the bad stuff from the string. 我宁愿拥有的是一个从字符串中删除所有不良内容的函数。 I tried to basically replace preg_match with preg_replace but no cigar. 我试着基本上用preg_replace替换preg_match但没有雪茄。

Following Gordon's reference , this gives: 根据戈登的参考 ,这给出了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM