【问题标题】:The /e modifier is deprecated, use preg_replace_callback instead after migrating from php5.2.6/e 修饰符已弃用,请在从 php5.2.6 迁移后改用 preg_replace_callback
【发布时间】:2016-11-20 19:12:43
【问题描述】:

我最近从 php5.2.6 迁移到 php5.6.22,现在出现此错误。

Unkwown error. 8192: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

似乎preg_replace 在 php5.6++ 中已被弃用

http://php.net/manual/en/migration55.deprecated.php

这是我使用 `preg_replace 函数的整个函数:

function mb_unserialize( $serial_str ) {
    $out = preg_replace( '!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $serial_str );
    return unserialize( $out );
}

有人可以解释我应该如何使用这种模式实现preg_replace_callback 函数吗? preg_replace_callback 在这种情况下是如何工作的?

谢谢

【问题讨论】:

  • preg_replace 未被弃用非 PCRE e (ereg) 标志 ,以及 ereg_replace - 任何Perl 兼容的 RegEx 很好。
  • 感谢您的回答,所以基本上我只需要将preg_replace 模式从'!s:(\d+):"(.*?)";!se' 改进为'/!s:(\d+):"(.*?)";!se/' 并且不需要使用preg_replace_callback 吗?
  • 在您的原始模式中,! 是正则表达式匹配的开始和结束 - 最后的 se 字符是模式修饰符 (php.net/manual/en/reference.pcre.pattern.modifiers.php) - 它是 e 修饰符已不复存在。您的模式可能应该是 /s:(\d+):"(.*?)";/s(尽管如果您也需要 s 修饰符,我会感到惊讶)。
  • 您建议的模式 /s:(\d+):"(.*?)";/s 没有按预期工作,必须离开 ! 才能正确输出。所以最后patter是'/!s:(\d+):"(.*?)";!/s',再次感谢cmets。
  • 啊,对,没有意识到 ! 是模式的一部分 ;) 以为它们是分隔符(它们是这样的)。

标签: php preg-replace deprecated preg-replace-callback


【解决方案1】:

正式回答:

function mb_unserialize( string $serial_str ):string {
    return unserialize( preg_replace_callback(
        '/s:(?:\d+):"(.*?)";/s', // -- the first pair of parentheses is not used.
        function( array $matches ): string { // -- this is the callback.
            return 's:' . strlen( $matches[1] ) . ':"' . $matches[1] . '";';
        },
        $serial_str
    ));
}

【讨论】:

    猜你喜欢
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 2013-03-05
    相关资源
    最近更新 更多