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 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!