我最近从php5.2.6迁移到了php5.6.22,现在我遇到了这个错误。
Unkwown error. 8192: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead
看起来在php5.6++中,preg_replace
已被弃用。
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兼容的正则表达式都可以。 - CD001preg_replace
的模式从'!s:(\d+):"(.*?)";!se'
改进为'/!s:(\d+):"(.*?)";!se/'
,而不需要使用preg_replace_callback
吗? - Adomas Kondrotas!
是 RegEx 匹配的起点和终点 - 最后的se
字符是模式修饰符 (http://php.net/manual/en/reference.pcre.pattern.modifiers.php) -e
修饰符已经过时。您的模式可能应该是/s:(\d+):"(.*?)";/s
(尽管我会惊讶如果您需要s
修饰符)。 - CD001/s:(\d+):"(.*?)";/s
没有按预期工作,必须加上!
才能得到正确的输出。因此最终的正则表达式是'/!s:(\d+):"(.*?)";!/s'
,再次感谢您的评论。 - Adomas Kondrotas!
是模式的一部分;)以为它们是定界符(它们实际上就像定界符)。 - CD001