奇怪的 php 正则表达式通知
我正在尝试编写一个函数来将错误的文件名转换为正确的文件名。我尝试使用正则表达式来完成此操作,它效果很好,但每次尝试更正名称时都会发出通知。这是我的代码:
private function clean_filename($filename) {
$reserved = preg_quote('\/:*?"<>|', '/');
$filename = preg_replace("/([\\x00-\\x20\\x7f-\\xff" .$reserved . "])/e", "_", $filename);
return $filename;
}
通知是:
注意:使用未定义常量 _ - 在 C:\Documents and Settings\A 中假定为“_” dministrator\Desktop\script\script.php(89) : 在线正则表达式代码 1
可能是什么问题?提前致谢!!
I'm trying to write a function for converting bad file names to correct file names. I tryed to accomplish this with regex, which works well but throws an notice every time try to correct a name. This is my code:
private function clean_filename($filename) {
$reserved = preg_quote('\/:*?"<>|', '/');
$filename = preg_replace("/([\\x00-\\x20\\x7f-\\xff" .$reserved . "])/e", "_", $filename);
return $filename;
}
The notice is:
Notice: Use of undefined constant _ - assumed '_' in C:\Documents and Settings\A
dministrator\Desktop\script\script.php(89) : regexp code on line
1
Wht could be the problem? Thanks in advance!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
e
强制将计算作为 PHP 表达式。因此,您必须使用:更好的是删除
e
标志,因为您不需要它(您的替换表达式是固定的;它始终是下划线字符)。Using
e
forces an evaluation as a PHP expression. So you have to use:Even better would be dropping the
e
flag instead as you don't need it (your replacement expression is fixed; it's always an underscore character).