当前位置:Gxlcms > PHP教程 > preg_replace的/e有关问题

preg_replace的/e有关问题

时间:2021-07-01 10:21:17 帮助过:5人阅读

preg_replace 的/e问题
由于新版PHP不能用/e,下面这句要怎么改才正常

preg_replace("/(?<=$so)\d+/eis", '$0 - '.$morefrf[6], $rs,1);
正常执行结果是:类别0::::::品名1::::::品牌厂商2::::::规格3::::::1::::::单位5::::::9::::::0
去掉/e,结果变成了:类别0::::::品名1::::::品牌厂商2::::::规格3::::::1::::::单位5::::::10-1::::::0

目的是让内容的原数量减新内容的数量,所以这10和1不是固定的.
------解决思路----------------------
对于
$morefrf[6]=1;
preg_replace("/(?<=$so)\d+/eis", '$0 - '.$morefrf[6], $rs,1);

要改写为
$morefrf[6]=1;
preg_replace_callback("/(?<=$so)\d+/is", function($m) use($morefrf) { return $m[0] - $morefrf[6]; }, $rs,1);

人气教程排行