PHP PCRE 错误 preg_replace

发布于 2024-10-05 01:27:07 字数 791 浏览 3 评论 0原文

<?php
    function pregForPreg($value)
    {
        $value = preg_replace(array('#\(#', '#\)#', '#\+#', '#\?#', '#\*#', '#\##', '#\[#', '#\]#', '#\&#', '#\/#', '#\$#', '#\\\\#'), array('\(', '\)', '\+', '\?', '\*', '\#', '\[', '\]', '\&', '\/', '\\\$', '\\\\'), $value);
        return $value;
    }

    $var = "TI - Yeah U Know [OFFCIAL VIDEO] [TAKERS] [w\LYRICS]";

    $var = pregForPreg($var);
    //$var is now:
    //    TI - Yeah U Know \[OFFCIAL VIDEO\] \[TAKERS\] \[w\LYRICS\]
    $var = preg_replace("#" . $var . "#isU", 'test', $var);
    echo $var;

我收到一个错误: *Warning: preg_replace(): Compilation failed: PCRE does not support \L, \l, \N, \U, or \u at offset 50 in test.php on line 13.*

如何制作正确的函数 pregForPreg?

<?php
    function pregForPreg($value)
    {
        $value = preg_replace(array('#\(#', '#\)#', '#\+#', '#\?#', '#\*#', '#\##', '#\[#', '#\]#', '#\&#', '#\/#', '#\$#', '#\\\\#'), array('\(', '\)', '\+', '\?', '\*', '\#', '\[', '\]', '\&', '\/', '\\\

And I get an error: *Warning: preg_replace(): Compilation failed: PCRE does not support \L, \l, \N, \U, or \u at offset 50 in test.php on line 13.*

How to make a correct function pregForPreg?

, '\\\\'), $value); return $value; } $var = "TI - Yeah U Know [OFFCIAL VIDEO] [TAKERS] [w\LYRICS]"; $var = pregForPreg($var); //$var is now: // TI - Yeah U Know \[OFFCIAL VIDEO\] \[TAKERS\] \[w\LYRICS\] $var = preg_replace("#" . $var . "#isU", 'test', $var); echo $var;

And I get an error: *Warning: preg_replace(): Compilation failed: PCRE does not support \L, \l, \N, \U, or \u at offset 50 in test.php on line 13.*

How to make a correct function pregForPreg?

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

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

发布评论

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

评论(1

裂开嘴轻声笑有多痛 2024-10-12 01:27:07

看来你想转义特殊的正则表达式字符。该函数已经存在,名为 preg_quote()


您会收到错误,因为您没有正确转义 \

TI - Yeah U Know \[OFFCIAL VIDEO\] \[TAKERS\] \[w\LYRICS\]
//                   this is not escaped   ------^

并且 \L 在 Perl 正则表达式中具有特殊含义:

\L 小写直到 \E


但PHP 的 PCRE 不支持(Perl 差异):

不支持以下 Perl 转义序列:\l、\u、\L、\U。事实上,这些是由 Perl 的通用字符串处理实现的,而不是其模式匹配引擎的一部分。

更新:

显然,您不能使用转义版本作为值和模式,因为在模式中 \[ 将被视为 [ 并且值 \[ 按字面意思理解。您必须将转义字符串存储在新变量中:

$var = "TI - Yeah U Know [OFFCIAL VIDEO] [TAKERS] [w\LYRICS]";

$escaped = preg_quote($var);
echo $escaped . PHP_EOL;
// prints "TI - Yeah U Know \[OFFCIAL VIDEO\] \[TAKERS\] \[w\\LYRICS\]"
$var = preg_replace('#' . $escaped . '#isU', 'test', $var);
echo $var;
// prints test

或更简单:

$var = preg_replace('#' . preg_quote($var) . '#isU', 'test', $var);

旁注:如果您确实想匹配字符串中的 \[ ,则正则表达式将为 \\\\\ [。你看,它可能会变得非常难看。

It seems you want to escape special regex characters. This function already exists and is called preg_quote().


You get the error, because you don't escape \ properly:

TI - Yeah U Know \[OFFCIAL VIDEO\] \[TAKERS\] \[w\LYRICS\]
//                   this is not escaped   ------^

and \L has special meaning in Perl regular expression:

\L Lowercase until \E

but is not supported in PHP's PCRE (Perl Differences):

The following Perl escape sequences are not supported: \l, \u, \L, \U. In fact these are implemented by Perl's general string-handling and are not part of its pattern matching engine.

Update:

Obviously, you cannot use the escaped version as value and as pattern, because in the pattern \[ will be treated as [ and but in the value \[ is taken literally. You have to store the escaped string in a new variable:

$var = "TI - Yeah U Know [OFFCIAL VIDEO] [TAKERS] [w\LYRICS]";

$escaped = preg_quote($var);
echo $escaped . PHP_EOL;
// prints "TI - Yeah U Know \[OFFCIAL VIDEO\] \[TAKERS\] \[w\\LYRICS\]"
$var = preg_replace('#' . $escaped . '#isU', 'test', $var);
echo $var;
// prints test

or easier:

$var = preg_replace('#' . preg_quote($var) . '#isU', 'test', $var);

Side note: If you really wanted to match \[ in a string, the regular expression would be \\\\\[. You see, it can get quite ugly.

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