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);
  • 写回答

1条回答 默认 最新

  • 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>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器