我正在尝试编写一个将错误文件名转换为正确文件名的函数。我尝试使用正则表达式来完成此操作,它运行良好但每次尝试更正名称时都会发出通知。这是我的代码:
private function clean_filename($filename) {
$reserved = preg_quote('\/:*?"<>|', '/');
$filename = preg_replace("/([\\x00-\\x20\\x7f-\\xff" .$reserved . "])/e", "_", $filename);
return $filename;
}
通知是:
Notice: Use of undefined constant _ - assumed '_' in C:\Documents and Settings\A dministrator\Desktop\script\script.php(89) : regexp code on line 1
可能是什么问题?提前致谢!!
最佳答案
使用 e
强制计算为 PHP 表达式。所以你必须使用:
$filename = preg_replace("/([\\x00-\\x20\\x7f-\\xff" .$reserved . "])/e",
"'_'", $filename); //or "\"_\""; or '"_"' etc.
更好的做法是删除 e
标志,因为您不需要它(您的替换表达式是固定的;它始终是下划线字符)。
$filename = preg_replace("/([\\x00-\\x20\\x7f-\\xff" .$reserved . "])/",
"_", $filename);
关于php - 奇怪的 PHP 正则表达式通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8400245/