PHP preg_replace 错误

发布于 2024-08-08 21:51:20 字数 1286 浏览 4 评论 0 原文

我有以下代码:

protected function safePath($path) {
        $path = (string) $path;

        $path = preg_replace(
            array(
            '#[\n\r\t\0]*#im',
            '#/(\.){1,}/#i',
            '#(\.){2,}#i',
            '#(\.){2,}#i',
            '#\('.DIRECTORY_SEPARATOR.'){2,}#i'
            ),
            array(
            '',
            '',
            '',
            '/'
            ),
            $path
            )
        ;
        return rtrim($path,DIRECTORY_SEPARATOR);
    }

在使用路径执行函数后,出现此错误:

警告:preg_replace()[function.preg-replace]:编译失败:<中偏移量3处的括号不匹配b>....../myfile.php 位于 534
行,

其中第 534 行是此处标有的行:

protected function safePath($path) {
        $path = (string) $path;

        $path = preg_replace(
            array(
            '#[\n\r\t\0]*#im',
            '#/(\.){1,}/#i',
            '#(\.){2,}#i',
            '#(\.){2,}#i',
            '#\('.DIRECTORY_SEPARATOR.'){2,}#i'
            ),
            array(
            '',
            '',
            '',
            '/'
            ),   <---------------- THis is line 534
            $path
            )
        ;
        return rtrim($path,DIRECTORY_SEPARATOR);
    }

修复此错误有任何帮助吗?谢谢。

I have the following code:

protected function safePath($path) {
        $path = (string) $path;

        $path = preg_replace(
            array(
            '#[\n\r\t\0]*#im',
            '#/(\.){1,}/#i',
            '#(\.){2,}#i',
            '#(\.){2,}#i',
            '#\('.DIRECTORY_SEPARATOR.'){2,}#i'
            ),
            array(
            '',
            '',
            '',
            '/'
            ),
            $path
            )
        ;
        return rtrim($path,DIRECTORY_SEPARATOR);
    }

After I execute the function with a path, I get this error:

Warning: preg_replace() [function.preg-replace]: Compilation failed: unmatched parentheses at offset 3 in ....../myfile.php on line 534

where line 534 is this one marked with here:

protected function safePath($path) {
        $path = (string) $path;

        $path = preg_replace(
            array(
            '#[\n\r\t\0]*#im',
            '#/(\.){1,}/#i',
            '#(\.){2,}#i',
            '#(\.){2,}#i',
            '#\('.DIRECTORY_SEPARATOR.'){2,}#i'
            ),
            array(
            '',
            '',
            '',
            '/'
            ),   <---------------- THis is line 534
            $path
            )
        ;
        return rtrim($path,DIRECTORY_SEPARATOR);
    }

Any help with fixing this error ? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

萌无敌 2024-08-15 21:51:20

在最后的正则表达式中,您已经转义了左括号,但不是右括号

'#\('.DIRECTORY_SEPARATOR.'){2,}#i'

可能应该是...

'#\('.DIRECTORY_SEPARATOR.'\){2,}#i'
                           ^
                           |
                       missing slash

...或者斜杠可能根本不应该在那里。无论哪种方式,其不一致。

in the final regex, you've escaped the opening parenthesis but not the closing one

'#\('.DIRECTORY_SEPARATOR.'){2,}#i'

should perhaps be...

'#\('.DIRECTORY_SEPARATOR.'\){2,}#i'
                           ^
                           |
                       missing slash

...or perhaps the slash shouldn't be there at all. Either way, its inconsistent.

鸵鸟症 2024-08-15 21:51:20

这是因为这部分就在这里:

    '#\('.DIRECTORY_SEPARATOR.'){2,}#i'

我猜你使用的是 Linux 系统。在 Windows 上,DIRECTORY_SEPARATOR 是一个反斜杠,它与该行上的初始反斜杠匹配。

在 Linux 上,DIRECTORY_SEPARATOR 是正斜杠,因此该字符串在开头有一个转义的左括号,而在结尾处没有匹配的括号。

您可以通过简单地用 /\ 替换该行上的 DIRECTORY_SEPARATOR 来在任一类型的操作系统上重现该错误。您将立即看到结果。

It's because of this part right here:

    '#\('.DIRECTORY_SEPARATOR.'){2,}#i'

I'm guessing you're on a linux system. On Windows, the DIRECTORY_SEPARATOR is a backslash, which matches with the initial backslash you have on that line.

On linux, the DIRECTORY_SEPARATOR is a forward slash, and therefore this string has an escaped left bracket at the start, and no matching bracket at the end.

You can reproduce the error on either type of OS by simply replacing the DIRECTORY_SEPARATOR on that line with either a / or a \. You'll see the result right away.

孤星 2024-08-15 21:51:20

很奇怪,如果两个 parethesees 都被转义,我只能得到这个错误:

'#\('.DIRECTORY_SEPARATOR.'\){2,}#i'

也许是因为你没有转义 DIRECTORY_SEPARATOR ?

'#\(\\'.DIRECTORY_SEPARATOR.'\){2,}#i'

Mighty odd, I can only get that error if both parethesees are escaped:

'#\('.DIRECTORY_SEPARATOR.'\){2,}#i'

Maybe because you are not escaping the DIRECTORY_SEPARATOR ?

'#\(\\'.DIRECTORY_SEPARATOR.'\){2,}#i'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文