doulan2827
doulan2827
2018-06-19 15:25
浏览 34
已采纳

有没有一种有效的方法在php中更大的正则表达式替换中运行子正则表达式替换语句

I am converting a handlebars template to be reusable within the compiled page.

Sample code:

{{title}} 
<div id="desktop">
    {{#each menu}}
        <a href="{{link}}">{{name}}</a>
    {{/each}}
</div>

On the compiled page, I am wanting

Search Engines
<div id="desktop">
    <a href="https://google.com">Google</a>
    <a href="https://yahoo.com">Yahoo</a>
    <script type="text/template" id="repeatable-menu"><!--
        <a href="{{link}}">{{name}}</a>
    --></script>
</div>

I am able to get halfway there by doing a regex replace on the template before it is compiled by handlebars and get the output

Search Engines
<div id="desktop">
    <a href="https://google.com">Google</a>
    <a href="https://yahoo.com">Yahoo</a>
    <script type="text/template" id="repeatable-menu"><!--
        <a href=""></a>
    --></script>
</div>

by using the regex statement:

$templateHTML = preg_replace('/({{#each (.*?)}}(.*?){{\\/each}})/s', '$1<script type="text/template" id="repeatable-$2"><!--$3--></script>', $templateHTML, -1);

Another alternative I have come up with is to break up the handlebars statements with something like this <a href="{|{link}|}">{|{name}|}</a> but I am unsure how to do that and preserve the {{title}} variable.

would it be the wisest to run this code in a while loop until no replacements are made?

$templateHTML = preg_replace('/(<script type="text\\/template" .*?\\><!--.*?){{(.*?--\\>\\<\\/script\\>)/s', '$1{|{$2', $templateHTML, -1, $count);

图片转代码服务由CSDN问答提供 功能建议

我正在将把手模板转换为可在编译页面中重复使用。

示例代码:

  {{title}} 
&lt; div id =“desktop”&gt; 
 {{#each menu}} 
&lt; a href =  “{{link}}”&gt; {{name}}&lt; / a&gt; 
 {{/ each}} 
&lt; / div&gt; 
   
 
 

在编译页面上,我想要

 搜索引擎
&lt; div id =“desktop”&gt; 
&lt; a href =“https://google.com  “&gt; Google&lt; / a&gt; 
&lt; a href =”https://yahoo.com“&gt; Yahoo&lt; / a&gt; 
&lt; script type =”text / template“id =”repeatable-menu“  &gt;&lt;! -  
&lt; a href =“{{link}}”&gt; {{name}}&lt; / a&gt; 
  - &gt;&lt; / script&gt; 
&lt; / div&gt;  
   
 
 

我可以通过在手柄编译之前对模板进行正则表达式替换来获得输出

 搜索引擎
&lt; div id =“desktop”&gt; 
&lt; a href =“https://google.com”&gt; Google&lt; / a&gt; 
&lt; a href =“  https://yahoo.com“&克 t; Yahoo&lt; / a&gt; 
&lt; script type =“text / template”id =“repeatable-menu”&gt;&lt;! -  
&lt; a href =“”&gt;&lt; / a&gt; \  n  - &gt;&lt; / script&gt; 
&lt; / div&gt; 
   
 
 

使用正则表达式语句:

   $ templateHTML = preg_replace('/({{#each(。*?)}}(。*?){{\\ / each}})/ s','$ 1&lt; script type =“text / 模板“id =”repeatable- $ 2“&gt;&lt;! -  $ 3  - &gt;&lt; / script&gt;',$ templateHTML,-1); 
   
 
 <  p>我提出的另一种选择是用这样的&lt; a href =“{| {link} |}”&gt; {| {name} |}&lt; / a&gt;来分解把手语句。  ; 但我不确定如何做到这一点并保留{{title}}变量。 
 
 

在while循环中运行此代码是否最明智,直到没有替换 是吗?

  $ templateHTML = preg_replace('/(&lt; script type =“text \\ / template”。*?\\&gt;&lt;! - 。  *?){{(。*? -  \\&gt; \\&lt; \\ / script \\&gt;)/ s','$ 1 {| {$ 2',$ templateHTML,-1,$ count);  
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drtldt55533
    drtldt55533 2018-06-19 17:00
    已采纳

    Instead of doing it in a single preg_replace, you could use preg_replace_callback to match the pattern for the template.

    Then loop through the links in the callback function.

    Example snippet:

    <?php
    
    $template = '{{title}} 
    <div id="desktop">
        {{#each menu}}
            <a href="{{link}}">{{name}}</a>
        {{/each}}
    </div>';
    
    $links = array(
        "Google"=>"https://google.com", 
        "Yahoo"=>"https://yahoo.com"
    );
    
    $result = preg_replace_callback(
                '/^\s*\{\{#each\s*([^{}]*)\}\}\s*^(.*?)^\s*\{\{\/each\}\}/sm',
                function ($matches) {
                    $eachloopscript = $matches[0];
                    $eachlooptitle  = $matches[1];
                    $eachlooplines  = $matches[2];
    
                    $scripttext = '<script type="text/template" id="repeatable-'.$eachlooptitle.'">';
    
                    global $links;
                    foreach($links as $x_key => $x_value) {
                        $temp = str_replace('{{link}}',$x_key,$eachlooplines);
                        $scripttext .= str_replace('{{name}}',$x_value,$temp);
                    }
    
                    $scripttext .= '<!--'.$eachloopscript.'--></script>';
    
                    return $scripttext;
                }, 
                $template);
    
    echo '<xmp>'.$result.'</xmp>';
    
    点赞 评论

相关推荐