Home >Backend Development >PHP Tutorial >preg_replace - ecshop在php5.6下的错误。

preg_replace - ecshop在php5.6下的错误。

WBOY
WBOYOriginal
2016-06-06 20:09:321248browse

ecshop在php5.6下的错误。在5.3下是正常的。应该是PHP高版本函数变化了。

错误提示:

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/wwwroot/www.ufeelwine.com/includes/cls_template.php on line 1061

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/wwwroot/www.ufeelwine.com/includes/cls_template.php on line 483

具体网页地址:
http://www.ufeelwine.com/supplier.php?suppId=7

第483行:

<code>                    $out = "<?php \n" . '$k = ' . preg_replace("/(\'\\$[^,]+)/e" , "stripslashes(trim('\\1','\''));", var_export($t, true)) . ";\n";
                    $out .= 'echo $this->_echash . $k[\'name\'] . \'|\' . serialize($k) . $this->_echash;' . "\n?>";
</code>

第1061行:

<code>            $pattern     = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/se';
            $replacement = "'{include file='.strtolower('\\1'). '}'";
            $source      = preg_replace($pattern, $replacement, $source);</code>

应该怎么改写呢?

回复内容:

ecshop在php5.6下的错误。在5.3下是正常的。应该是PHP高版本函数变化了。

错误提示:

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/wwwroot/www.ufeelwine.com/includes/cls_template.php on line 1061

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/wwwroot/www.ufeelwine.com/includes/cls_template.php on line 483

具体网页地址:
http://www.ufeelwine.com/supplier.php?suppId=7

第483行:

<code>                    $out = "<?php \n" . '$k = ' . preg_replace("/(\'\\$[^,]+)/e" , "stripslashes(trim('\\1','\''));", var_export($t, true)) . ";\n";
                    $out .= 'echo $this->_echash . $k[\'name\'] . \'|\' . serialize($k) . $this->_echash;' . "\n?>";
</code>

第1061行:

<code>            $pattern     = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/se';
            $replacement = "'{include file='.strtolower('\\1'). '}'";
            $source      = preg_replace($pattern, $replacement, $source);</code>

应该怎么改写呢?

//-- 原本

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

//-- 修改成

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

//-- 原本

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

//-- 修改成

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

//-- 原本

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

//-- 修改成

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

//--- 原本

<code>$pattern     = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/se';
$replacement = "'{include file='.strtolower('\\1'). '}'";
$source      = preg_replace($pattern, $replacement, $source);
</code>

//--- 修改成

<code>$pattern     = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/s';
$replacement = function ($r) { return '{include file='.strtolower($r[1]). '}'; };
$source      = preg_replace_callback($pattern, $replacement, $source);</code>

试试,没有环境没办法测试,根据提示信息改的

第483行:改成

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

第1061行:改成

<code>//$pattern     = '/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/se';
//$replacement = "'{include file='.strtolower('\\1'). '}'";
$source      = preg_replace('/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/se', function($r) { return '{include file='.strtolower('\\1'). '}';}, $source);</code>

提示很清楚了啊
把preg_replace换成preg_replace_callback
看看preg_replace_callback的用法就明白了

<code class="php">/*
1、e参数去掉
2、后面的\1换成回调的数组对应的元素
*/
//483行
$out = "<?php \n" . '$k = ' . preg_replace_callback("/(\'\\$[^,]+)/" ,function($r) { return stripslashes(trim($r[1],'\'')); }, var_export($t, true)) . ";\n";</code></code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn