Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用输出作为文件夹名称的最佳PHP加密/编码方法_Php_Encryption_Encoding_Directory_Special Characters - Fatal编程技术网

使用输出作为文件夹名称的最佳PHP加密/编码方法

使用输出作为文件夹名称的最佳PHP加密/编码方法,php,encryption,encoding,directory,special-characters,Php,Encryption,Encoding,Directory,Special Characters,我有一些少于18个字符的文本。我想用这个文本作为目录名创建一个目录。 有些时候,文本会有一些特殊的特征,如:a,é,í,ó,ú,ü,ñ?因此它不能用作目录名 因此,我认为最好对测试进行加密或编码,以便可以使用我们的文件夹名 哪种加密/编码方法最适合我 提前感谢如果你想使用散列,只需使用它,因为它速度非常快,而且你不需要任何加密功能强大的东西 或者您可以轻松清理字符串,以下是我的方法: /** * Sanitizes a filename, replacing whitespace with d

我有一些少于18个字符的文本。我想用这个文本作为目录名创建一个目录。 有些时候,文本会有一些特殊的特征,如:a,é,í,ó,ú,ü,ñ?因此它不能用作目录名

因此,我认为最好对测试进行加密或编码,以便可以使用我们的文件夹名

哪种加密/编码方法最适合我


提前感谢

如果你想使用散列,只需使用它,因为它速度非常快,而且你不需要任何加密功能强大的东西

或者您可以轻松清理字符串,以下是我的方法:

/**
 * Sanitizes a filename, replacing whitespace with dashes and transforming the string to lowercase.
 *
 * Removes special characters that are illegal in filenames on certain operating systems and special characters
 * requiring special escaping to manipulate at the command line. Replaces spaces and consecutive dashes with a single
 * dash. Trims period, dash und underscore from beginning and end of filename.
 *
 * @param string $filename
 *   The filename to be sanitized.
 * @return string
 *   The sanitized filename.
 * @throws \InvalidArgumentException
 *   If <var>$filename</var> is invalid.
 */
final public static function sanitizeFilename($filename) {
  if (empty($filename)) {
    throw new \InvalidArgumentException("A file's name cannot be empty.");
  }

  // Remove characters which aren't allowed in filenames.
  $filename = str_replace([ "?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", '"', "&", "$", "#", "*", "(", ")", "|", "~" ], "", $filename);

  // Replace whitespace characters with dashes.
  $filename = preg_replace("/[\s-]+/", "-", $filename);

  // Remove characters which aren't allowed at the beginning and end of a filename.
  $filename = trim($filename, ".-_");

  // Always lowercase all filenames for better compatibility.
  return mb_strtolower($filename);
}
/**
*清理文件名,用破折号替换空白,并将字符串转换为小写。
*
*删除某些操作系统上文件名中非法的特殊字符和特殊字符
*需要特殊转义才能在命令行进行操作。将空格和连续破折号替换为单个
*冲刺。从文件名的开头和结尾修剪句点、破折号和下划线。
*
*@param string$filename
*要清除的文件名。
*@返回字符串
*已清除的文件名。
*@throws\InvalidArgumentException
*如果$filename无效。
*/
最终公共静态函数sanitizeFilename($filename){
if(空($filename)){
抛出new\InvalidArgumentException(“文件名不能为空”);
}
//删除文件名中不允许的字符。
$filename=str_replace([“?”、“[”、“]”、“/”、“\\”、“=”、“:”、“;”、“、”、“、”、“&“、”、“$”、“#”、“*”、“(“、”、”、“、”、“~”、“、”、$filename);
//将空白字符替换为破折号。
$filename=preg_replace(“/[\s-]+/”、“-”、$filename);
//删除文件名开头和结尾不允许的字符。
$filename=trim($filename,“.-”);
//始终将所有文件名小写,以实现更好的兼容性。
返回mb_strtolower($filename);
}
如果您关心重新提取原始文件夹名称,我认为可以使用编码:

base64_encode('Folder Name');   // results: Rm9sZGVyIE5hbWU=
如果不想恢复原始名称,可以使用:

md5('Folder Name');   // results: d89dbf99916d31a7870474d44d481ffa