Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP正则表达式{{}_Php_Regex - Fatal编程技术网

PHP正则表达式{{}

PHP正则表达式{{},php,regex,Php,Regex,我在为preg_match编写PHP正则表达式时遇到了一些困难。基本上,我需要匹配以下模式: {{anything}} {{{anything}}} 但这是最简单的部分。如果字符串仅包含此内容,我想将此类表达式替换为“”(空字符串),但如果字符串包含其他内容,我需要保留,因此以下字符串将变为有效: this is about {{anything}} {{{anything}}} anywhere 非常感谢您的帮助:)。请使用锚定 preg_replace('~^\{+[^{}]*\}+$\

我在为preg_match编写PHP正则表达式时遇到了一些困难。基本上,我需要匹配以下模式:

{{anything}}
{{{anything}}}
但这是最简单的部分。如果字符串仅包含此内容,我想将此类表达式替换为“”(空字符串),但如果字符串包含其他内容,我需要保留,因此以下字符串将变为有效:

this is about {{anything}}
{{{anything}}} anywhere
非常感谢您的帮助:)。

请使用锚定

preg_replace('~^\{+[^{}]*\}+$\n?~m', '', $str);
您也可以使用


看起来断线没用。请注意{{}周围有换行符。首先检查字符串是否包含您在说“如果字符串仅包含此内容”时所查找的任何“this”,然后如果是这种情况,请使用preg\u replace以
\}+
结尾
pattern('\{+[^{}]*\}+')->replace()->all()->callback(function (Match $m) {
    if ($m->text() === 'anything') {
        return '';
    }
    return $m->text();
});