【问题标题】:strange php regex notice奇怪的 php 正则表达式通知
【发布时间】:2012-01-14 01:47:37
【问题描述】:

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

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

可能是什么问题?提前致谢!!

【问题讨论】:

    标签: php regex notice


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      相关资源
      最近更新 更多