在if正则表达式的帮助下,您可以创建一个模式,并对字符串执行非贪婪搜索,并用为每个匹配添加的包装器替换每个匹配。
function wrapVariables($subject,$opening_tags,$closing_tags){
$matches = [];
preg_match_all("/\{\{.*\}\}/U",$subject,$matches);
$map_for_matches = [];
foreach($matches[0] as $each_match){
if(!isset($map_for_matches[$each_match])){
$replace_value = $opening_tags . $each_match . $closing_tags;
$subject = str_replace($each_match,$replace_value, $subject);
$map_for_matches[$each_match] = 1;
}
}
return $subject;
}
$subject = "<P> {{ variable | money_with_currency }} </P><P> {{variable | money }} something </P><P> {{ variable | filter}} </P><P> {{variable | filter}} </P><P> {{ variable | money_with_currency }} </P>";
echo wrapVariables($subject,"<b class='xyz'>","</b>");
输出
<P> <b class='xyz'>{{ variable | money_with_currency }}</b> </P>
<P> <b class='xyz'>{{variable | money }}</b> something </P>
<P> <b class='xyz'>{{ variable | filter}}</b> </P>
<P> <b class='xyz'>{{variable | filter}}</b> </P>
<P> <b class='xyz'>{{ variable | money_with_currency }}</b> </P>
使现代化
正则表达式模式匹配
<p>
和
<b>
标记,因为您没有提到文本的范围。
这里,正则表达式模式首先匹配
<p>
标签->过滤其中的每一个->执行的模式匹配
variable
在每个过滤器上跟踪已添加的
wrappers
例如
<b class='xyz'></b>
.
<?php
function wrapVariables($subject,$opening_tags,$closing_tags,$variable_to_replace){
$new_subject = "";
$matches = [];
preg_match_all("/<[pP]>(<.+>)?.+(<\/.+>)?<\/[pP]>/U",$subject,$matches);
foreach($matches[0] as $each_match){
$new_matches = [];
preg_match_all("/(<b class='xyz'>)?\\s*\{\{[^\\w]*".$variable_to_replace."[^\\w].*\}\}\\s*(<\/b>)?/U",$each_match,$new_matches);
if(!empty($new_matches[0][0]) && empty($new_matches[1][0])) $new_subject .= str_replace($new_matches[0][0],$opening_tags . $new_matches[0][0] . $closing_tags,$each_match);
else $new_subject .= $each_match;
}
return $new_subject;
}
$subject = "<P> {{ variable | money_with_currency }} </P><P> <b class='xyz'> {{ variable| money_with_currency }} </b></P><P> {{ |variable| money_with_currency }} </P><P> <b class='xyz'> {{ |variable money_with_currency }} </b></P><P> {{ variable | filter }} </P><P><b class='xyz'>{{ variable | filter }}</b></P><P>{{ InnervariableInner | money_with_currency }} </P><P> {{variablePrefix | money_with_currency }} </P><P> {{Suffixvariable| money_with_currency }} </P><P>{{Suffixvariable| money_with_currency }}</P><P> {{ variable2 | money_with_currency }} </P><P> {{ variable9 | money_with_currency }} </P>";
echo wrapVariables($subject,"<b class='xyz'>","</b>","variable");
输入
<P> {{ variable | money_with_currency }} </P>
<P> <b class='xyz'> {{ variable| money_with_currency }} </b></P>
<P> {{ |variable| money_with_currency }} </P>
<P> <b class='xyz'> {{ |variable money_with_currency }} </b></P>
<P> {{ variable | filter }} </P>
<P><b class='xyz'>{{ variable | filter }}</b></P>
<P>{{ InnervariableInner | money_with_currency }} </P>
<P> {{variablePrefix | money_with_currency }} </P>
<P> {{Suffixvariable| money_with_currency }} </P>
<P>{{Suffixvariable| money_with_currency }}</P>
<P> {{ variable2 | money_with_currency }} </P>
<P> {{ variable9 | money_with_currency }} </P>
输出
<P><b class='xyz'> {{ variable | money_with_currency }}</b> </P>
<P> <b class='xyz'> {{ variable| money_with_currency }} </b></P>
<P><b class='xyz'> {{ |variable| money_with_currency }}</b> </P>
<P> <b class='xyz'> {{ |variable money_with_currency }} </b></P>
<P><b class='xyz'> {{ variable | filter }}</b> </P>
<P><b class='xyz'>{{ variable | filter }}</b></P>
<P>{{ InnervariableInner | money_with_currency }} </P>
<P> {{variablePrefix | money_with_currency }} </P>
<P> {{Suffixvariable| money_with_currency }} </P>
<P>{{Suffixvariable| money_with_currency }}</P>
<P> {{ variable2 | money_with_currency }} </P>
<P> {{ variable9 | money_with_currency }} </P>