关于php:PHP7-不再支持/ e修饰符,请改用preg_replace_callback

PHP7 - The /e modifier is no longer supported, use preg_replace_callback instead

本问题已经有最佳答案,请猛点这里访问。

有人可以帮助我解决这个错误吗?

Warning: preg_replace(): The /e modifier is no longer supported, use
preg_replace_callback instead

我的原始代码:

1
$match[1] = preg_replace('/(?<=^|[\\x09\\x20\\x2D])./e', 'strtoupper("\\0")', strtolower(trim($match[1])));

所以我这样尝试:

1
2
3
4
5
6
7
$match[1] = preg_replace_callback('/(?<=^|[\\x09\\x20\\x2D])./e',
                    function ($matches) {
                        foreach ($matches as $match) {
                            return strtoupper($match);
                        }
                    },
                    strtolower(trim($match[1])));

但是我仍然遇到相同的错误:

Warning: preg_replace_callback(): The /e modifier is no longer
supported, use preg_replace_callback instead


错误消息告诉您删除新代码中包含的e修饰符。

1
2
3
4
/ (?<=^|[\\x09\\x20\\x2D]). / e
^ ^------Pattern-------^ ^ ^ ---- Modifiers
|                        |
 -------Delimiters-------

您需要删除修饰符,因此preg_replace_callback('/(?<=^|[\\x09\\x20\\x2D])./e', ...)应该为preg_replace_callback('/(?<=^|[\\x09\\x20\\x2D])./' , ...)

顺便说一句,您没有从在新代码中使用foreach循环中受益。 匹配项始终位于数组的第二项中。 这是不使用循环的示例:

1
2
3
4
5
6
7
8
9
10
$inputString = 'foobazbar';

$result = preg_replace_callback('/^foo(.*)bar$/', function ($matches) {
     // $matches[0]:"foobazbar"
     // $matches[1]:"baz"
     return"foo" . strtoupper($matches[1]) ."bar";
}, $inputString);

//"fooBAZbar"
var_dump($result);