С помощью регулярного выражения 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>