Ad

Want To Replace {{ Variable | Filter }} Or Warp This Value With Some Html Tag

- 1 answer

  1. <P> {{ variable | money_with_currency }} </P>
  2. <P> {{variable1 | money }} something </P>
  3. <div> {{ variable | filter}} </div>
  4. <P><b class="xyz"> {{ variable | filter }} </b></P>

output:

  1. <P><b class="xyz"> {{ variable | money_with_currency }} </b></P>

  2. <P> {{ variable1 | money }} something </P>

  3. <div><b class="xyz"> {{ variable | filter }} </b></div>

  4. <P><b class="xyz"> {{ variable | filter }} </b></P>

I want to wrap when variable find between the {{ }} but if it is already wrap than no need to wrap for 2nd time.

in my case I want to wrap if {{ }} has "variable" and wrap with

<b class="xyz"> {{ inner statement that not change only check for existence }} </b>

line 1 we need to wrap this one and see in the output

line 2 we no need to wrap because {{ }} has variable 1 which is differ

line 3 we need to wrap {{ }} has variable

line 4 we no need to wrap because it is already wrap with

Ad

Answer

With the help if regular expression, you can create a pattern and perform a non-greedy search on your string and replace each match with added wrappers for each match.

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>");

OUTPUT

<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>

UPDATE

Regex patterns match <p> and <b> tags specifically since you haven't mentioned what would be the scope of the text.

Here, regex pattern first matches <p> tags-> filters each of them-> performs pattern matching of variable on each filter keeping track of already added wrappers such as <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");

INPUT

<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>

OUTPUT

<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>
Ad
source: stackoverflow.com
Ad