dongtang3155 2019-07-21 19:47
浏览 96
已采纳

用递归和正则表达式替换字符串中的文本

I am using tags to replace text before displaying output in a browser, similar to Wordpress' short codes.

Example string: Hi, this is a block of text {{block:welcome}} and this is a system variable {{variable:system_version}}

I have functions to replace these blocks accordingly, and I realize a foreach or while function will be the best way to deal with it, but unfortunately, replacing one {{...}} may introduce another. Hence, I opted for recursion until no more are found. Typical recursion is only once, but I have had two in one scenario. Maybe calling the function 3 times will work, but it sounds "wrong".

Now that is where the problem occurs: I do NOT want to replace them when they appear in:

1) A page where the URL you are calling contains something
2) Any form element such as `<input>` or `<textarea>`.

I need help on how to exclude from #2 above by means of a regex.

My regex currently look like this: ^\{\{((?!keep).)*$ (I realize it may still be wrong, or need modification - does not quite work yet).

If the item contains "keep", e.g., {{block:welcome:keep}} it should not be replaced, but when doing so, the recursion never stops, as I keep finding items to replace, and thus run out of memory, or get maximum nested level errors.

The reason why I want to do this, is because I do not want the content replaced when on an ADMIN page, or when you are editing form content.

Someone willing to give it a crack? I am using PHP, if that matters.

Thanks!

EDIT 1

Since @Pablo's answer was given to me in chat, I decided to edit my question to reflect why his answer was marked as the correct one.

My regex now look like this: /(?:<(?:textarea|select)[\s\S]*?>[\s\S]*?)?({{variable:(.*?)}})[\s\S]*?(?:<\/(?:textarea|select)>)?|(?:<(?:input)[\s\S]*?)?{{variable:(.*?)}}(?:[\s\S]*?>)?/im

I then check if the match contains an input, select or textarea, and if so, replace the {{ with something else temporarily, and then do my replacement, and when done, change the "something else" back to {{ as Pablo suggested. My regex is thanks to the answer on this question: Text replacement: PHP/regex.

If the above edit does not belong, feel free to remove.

  • 写回答

1条回答 默认 最新

  • donglian2106 2019-07-21 21:50
    关注

    Instead of looking for the perfect RegEx I suggest looking into using preg_replace_callback(). It should allow you to use a simpler RegEx while having more control over the search and replace algorithm for your templating engine. Consider the following example:

    1. resolvePlaceholder() generates the replacing content
    2. interpolate() parses a template string. It supports nested parsing up to 4 levels.
    3. Stop recursive parsing for tags starting with !.

    <?php
    
    function resolvePlaceholder($name)
    {
        $store = [
            'user:first'              => 'John',
            'user:last'               => 'Doe',
            'user:full_name'          => '{{user:first}} {{user:last}}',
            'block:welcome'           => 'Welcome {{user:full_name}}',
            'variable:system_version' => '2019.1',
            'nest-test'               => '{{level1}}',
            'level1'                  => '{{level2}}',
            'level2'                  => '{{level3}}',
            'level3'                  => '{{level4}}',
            'level4'                  => '{{level5}}',
            'level5'                  => 'Nesting Limit Test Failed',
            'user-template'           => 'This is a user template with {{weird-placeholder}} that will not be replaced in edit mode {{user:first}}',
        ];
    
        return $store[$name] ?? '';
    }
    
    function interpolate($text, $level = 1)
    {
        // Limit interpolation recursion
        if ($level > 5) {
            return $text;
        }
    
        // Replace placeholders
        return preg_replace_callback('/{{([^}]*)}}/', function ($match) use ($level) {
            list($tag, $name) = $match;
            // Do not replace tags with :keep
            if (strpos($name, ':keep')) {
                // Remove :keep?
                return $tag;
            }
    
            if (strpos($name, '!') === 0) {
                return resolvePlaceholder(trim($name, '!'));
            }
    
            return interpolate(resolvePlaceholder($name), $level + 1);
        }, $text);
    }
    
    $sample = 'Hi, this is a block of text {{block:welcome}} and this is a system variable {{variable:system_version}}. ' .
        'This is a placeholder {{variable:web_url:keep}}. Nest value test {{nest-test}}. User Template: {{!user-template}}';
    
    echo interpolate($sample);
    // Hi, this is a block of text Welcome John Doe and this is a system variable 2019.1. This is a placeholder {{variable:web_url:keep}}. Nest value test {{level5}}. User Template: This is a user template with {{weird-placeholder}} that will not be replaced in edit mode {{user:first}}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?