Function create_function() is deprecated

我就是我 2022-12-02 13:50 323阅读 0赞

问题描述:

上午运行一个旧的php项目时报错:

  • PHP message: PHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead
  • Warning: preg_replace_callback(): Requires argument 2, 'iconv('UCS-2', 'UTF-8',
  • Function create_function() is deprecated

原因分析:

  • php 5.6之后的版本不再支持pre_replace()函数
  • 自PHP 7.2起,函数create_function因为代码注入漏洞已被弃用。从PHP 5.3开始,执行此操作的首选方法是使用匿名函数。要捕获外部变量的值,请使用use声明。

解决方案:

  1. preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", json_encode($data));

替换成:

  1. preg_replace_callback('/\\\\u([0-9a-f]{4})/i', function($matches){ return iconv("UCS-2BE","UTF-8",pack("H*", $matches[1]));}, json_encode($data));

或直接封装为一个函数,可实现更好地复用:

  1. function decodeUnicode($str){
  2. return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', function($matches){ return iconv("UCS-2BE","UTF-8",pack("H*", $matches[1]));}, $str);
  3. }

参考文章链接:

  • https://blog.csdn.net/crabdave/article/details/84810312
  • https://blog.csdn.net/weixin_34184561/article/details/92084636

发表评论

表情:
评论列表 (有 0 条评论,323人围观)

还没有评论,来说两句吧...

相关阅读