奇怪的 php 正则表达式通知

发布于 2024-12-19 16:18:24 字数 513 浏览 2 评论 0原文

我正在尝试编写一个函数来将错误的文件名转换为正确的文件名。我尝试使用正则表达式来完成此操作,它效果很好,但每次尝试更正名称时都会发出通知。这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许仙没带伞 2024-12-26 16:18:24

使用 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);

Using e forces an evaluation as a PHP expression. So you have to use:

$filename = preg_replace("/([\\x00-\\x20\\x7f-\\xff" .$reserved . "])/e",
    "'_'", $filename); //or "\"_\""; or '"_"' etc.

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).

$filename = preg_replace("/([\\x00-\\x20\\x7f-\\xff" .$reserved . "])/",
    "_", $filename);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文