ECSHOP如何解决Deprecated: preg_replace()报错ECSHOP如何解决Deprecated: preg_replace()报错

作者:csuper 发表于:2020-03-04

把php从原来的5.3升级到了现在的5.6,当初没有升级php环境一个重要原因就是不想去修改各个程序对php5.6的兼容代码,引起这个话题的主要原因是php5.6丢弃了5.3版本的很多参数和书写规范,导致原来的代码运营在5.6版本上出错。今天就细细给大家说说我在搬迁百蔬网时遇到的几个问题,和大家共勉。

ECSHOP如何解决Deprecated: preg_replace()报错ECSHOP如何解决Deprecated: preg_replace()报错

preg_replace函数是蛮多地方需要修改的一个函数,改动最多的是它,特别是后台很多地方都是提示这个函数的/e表达符弃用了,需要规范。大多数是在includes/cls_template.php文件。

includes/cls_template.php

错误提示:

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in includes/cls_template.php on line 300

解决办法:
300行

return preg_replace("/{([^\}\{\n]*)}/e", "\$this->select('\\1');", $source);

替换为

return preg_replace_callback("/{([^\}\{\n]*)}/", function($r) { return $this->select($r[1]); }, $source);

497行

 $out = "<?php \n" . '$k = ' . preg_replace("/(\'\\$[^,]+)/e" , "stripslashes(trim('\\1','\''));", var_export($t, true)) . ";\n";

修改为

$out = "<?php \n" . '$k = ' . preg_replace_callback("/(\'\\$[^,]+)/" , function($match){return stripslashes(trim($match[1],'\''));}, var_export($t, true)) . ";\n";

556行

$val = preg_replace("/\[([^\[\]]*)\]/eis", "'.'.str_replace('$','\$','\\1')", $val);

替换为

$val = preg_replace_callback('/\[([^\[\]]*)\]/is',function ($matches) {return '.'.str_replace('$','\$',$matches[1]);},$val);

1074行,这一行的修改是下一个preg_replace替换成功的关键,必须去掉pattern中的表达符/e

$pattern = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/se';

修改为

$pattern = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/s';

1076行

$source = preg_replace($pattern, $replacement, $source);

替换为

$source =preg_replace_callback($pattern, function($matcheb){ return '{include file='.strtolower($matcheb[1]). '}';}, $source);

mobile\include\vendor\phpmailer\class.phpmailer.php

如果有使用ectouch作为ecshop的手机移动客服端,那么还要修改这个文件mobile\include\vendor\phpmailer\class.phpmailer.php,这个文件有多个preg_replace函数使用了/e参数,具体如下:
1703行

$encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);

修改为

$encoded =preg_replace_callback("/([^A-Za-z0-9!*+\/ -])/", function($ra) { return '='.sprintf('%02X', ord($ra[1]));}, $encoded);

1707行

$encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);

修改为

$encoded = preg_replace_callback("/([\(\)\"])/", function($rb) { return '='.sprintf('%02X', ord($rb[1]));}, $encoded);

1713行

$encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e', "'='.sprintf('%02X', ord('\\1'))", $encoded);

修改为

$encoded = preg_replace_callback('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/', function($rc) { return '='.sprintf('%02X',ord($rc[1]));}, $encoded);

 

这个文件里面还存在一个书写规范的问题

错误提示

 Strict Standards: Only variables should be passed by reference in  \includes\cls_template.php on line 418

解决办法:
423行:
$tag_sel = array_shift(explode(' ', $tag)); 
php5.6已经不允许这样相套使用了,更换为
$tag_arr = explode(' ', $tag); $tag_sel = array_shift($tag_arr);

\mobile\include\library\EcsTemplate.class.php

248行

return preg_replace("/{([^\}\{\n]*)}/e", "\$this->select('\\1');", $source);

修改为

return preg_replace_callback("/{([^\}\{\n]*)}/", function($r) { return $this->select($r[1]);}, $source);

417行

$out = "<?php \n" . '$k = ' . preg_replace("/(\'\\$[^,]+)/e", "stripslashes(trim('\\1','\''));", var_export($t, true)) . ";\n";

修改为

$out = "<?php \n" . '$k = ' . preg_replace_callback("/(\'\\$[^,]+)/", function($r) { return stripslashes(trim($r[1],'\''));}, var_export($t, true)) . ";\n";

475行

$val = preg_replace("/\[([^\[\]]*)\]/eis", "'.'.str_replace('$','\$','\\1')", $val);

修改为

$val = preg_replace_callback("/\[([^\[\]]*)\]/is", function($r) { return '.'.str_replace('$','\$',$r[1]);}, $val);

912行

$pattern = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/se';

修改为

$pattern = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/s';

914行

$source = preg_replace($pattern, $replacement, $source);

修改为

$source =preg_replace_callback($pattern, function($matcheb){ return '{include file='.strtolower($matcheb[1]). '}';}, $source);

\mobile\include\common.php

662行

return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name));

修改为

return ucfirst(preg_replace_callback("/_([a-zA-Z])/", function($r) { return strtoupper($r[1]);}, $name));

另外一个函数相套使用需要修改的文件是 includes/lib_main.php
1330行
$ext = end(explode('.', $tmp));
修改为
$exta = explode('.', $tmp);$ext = end($exta);

还有php5.6版本弃用了split函数,需要修改,具体文件为goods.php

错误提示:

Function split() is deprecated Error File: \goods.php on line 362

解决办法:
362行将
$cat_ar=split('/',$cat_name);
修改为
$cat_ar=explode('/',$cat_name);
需要正则表达式的就需要更换为preg_split函数,速度慢些。

 


版权声明

本文仅代表作者观点,不代表Csuper立场。
本文系作者授权百度百家发表,未经许可,不得转载。