Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

PHP正则数组后循环替换,相同参数重复替换的问题

$strAll = "一堆字符aa  =  11;一堆字符bb  =  aa;一堆字符cc  =  bb;一堆字符bb  =  33; 一堆字符ff  =  bb ";
//已通过正则过滤成了数组,经过循环
foreach ($arrstr as $key=>$value) {
    $search  = array('='.$key);
    $replace = array('='.$value);
    $strAll = str_replace_limit($search,$replace,$strAll);
}

下面是实现的例子:

//key = value
aa  =  11;  跳过

bb  =  aa;  替换后值11

cc  =  bb;  替换后aa

dd  =  aa;  替换后值11

循环替换条件  = key 替换成 = value

上面的没问题,问题是下面这样的:

//key = value
aa  =  11;  跳过

bb  =  aa;  替换后值11

cc  =  bb;  先替换成了aa,后又被替换成了33

bb  =  33;  跳过

ff  =  bb;  先替换成了aa,后又被替换成了33

循环替换条件  = key 替换成 = value

这不是想要的结果,想要的是 cc = aa ,ff= 33,因为存在相同key ,就会导致最终被替换成了一样的值。

帮帮忙吧,可打赏。。。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...