代码之家  ›  专栏  ›  技术社区  ›  Sachin Sarola

想用一些html标记替换{variable | filter}或扭曲这个值吗

  •  2
  • Sachin Sarola  · 技术社区  · 6 年前
    1. <P>{{variable | money _with _currency}}</P>
    2. <P>{{variable1 | money}}something</P>
    3. <部门>{{variable | filter}}</部门>
    4. <P>&书信电报;b class=“xyz”>{{variable | filter}}</b></P>

    输出:

    1. <P>&书信电报;b class=“xyz”>{{variable | money _with _currency}}</b></P>

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

    3. <部门>&书信电报;b class=“xyz”>{{variable | filter}}</b></部门>

    4. <P>&书信电报;b class=“xyz”>{{variable | filter}}</b></P>

    我想在变量find位于{{}之间时进行换行,但如果它已经换行,则无需第二次换行。

    在我的例子中,如果{{}有“variable”,我想用

    <b class=“xyz”> {{不改变的内部语句只检查存在性}</b>

    第1行我们需要把这一行包装起来,看看输出

    第2行我们不需要换行,因为{{}有不同的变量1

    第3行我们需要包装{{}has变量

    第4行我们不需要包装,因为它已经用

    1 回复  |  直到 6 年前
        1
  •  2
  •   nice_dev    6 年前

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