我的字符串包含%somestring1:somestring2%
之类的字符串。我想用另一个字符串替换这些字符串,如果它的格式为%---:-----%
例如,
$str="test content %list:UnsubscriptionLink% some other %list:subscriptionLink% test";
我想用'#'替换存在。将是
$str="test content # some other # test";
怎么可能?
注意
我无法预测somestring1,somestring2.it是动态的。
$str="test content %list:UnsubscriptionLink% some other test";
echo preg_replace('~(%.*%)~','#',$str); //Its working
但是
$str="test content %list:UnsubscriptionLink% some other %list:UnsubscriptionLink% test";
echo preg_replace('~(%.*%)~','#',$str); //Its Not working
答案 0 :(得分:2)
为您提供更新的问题
<?php
$str="test content %list:UnsubscriptionLink% some other %list:UnsubscriptionLink% test";
echo preg_replace('~(%[^%]+%)~','#',$str);
<强>输出强>:
test content # some other # test
答案 1 :(得分:1)
~%[^%]+%~
<?php
$str="test content %list:UnsubscriptionLink% some other %list:UnsubscriptionLink% test";
echo preg_replace('~%[^%]+%~','#',$str);
?>
test content # some other # test