如何在preg_replace中报价替换?

时间:2021-11-03 16:54:13

E.g. in the code below, if the user wants the new content of the template to be the string C:\Users\Admin\1, the \1 part will become BEGIN this was the original content of the template END, which is something I don't want.

例如,在下面的代码中,如果用户想要模板的新内容的字符串C:\Users\Admin\ 1 \ 1部分将成为开始这是模板的原创内容结束,这是我不想要的东西。

preg_replace('/(BEGIN.*?END)/su', $_POST['content'], $template);

1 个解决方案

#1


3  

In short, use this function to quote a dynamic replacement pattern:

简而言之,使用此函数引用动态替换模式:

function preg_quote_replacement($repl_str) {
    return str_replace(array('\\', '$'), array('\\\\', '\\$'), $repl_str);
}

The thing is, you need to escape the backslash in the replacement pattern. See preg_replace docs:

问题是,您需要在替换模式中转义反斜杠。看到preg_replace文档:

To use backslash in replacement, it must be doubled ("\\\\" PHP string).

要使用反斜杠替换,它必须加倍(“\\\\ \\\”PHP字符串)。

It can be done with a mere str_replace function:

只需一个str_replace函数即可完成:

$repl = 'C:\Users\Admin\1';
$template = "BEGIN this was the original content of the template END";
echo preg_replace('/(BEGIN.*?END)/su', str_replace('\\', '\\\\', $repl), $template);

See IDEONE demo

看到IDEONE演示

However, NOTE that the $ symbol is also special in the replacement pattern. Thus, we also need to escape this symbol. The order of these prelimnary replacements matter: first, we need to escape the \, and then the $:

但是,请注意,$符号在替换模式中也是特殊的。因此,我们也需要逃离这个符号。这些预备的替换物的顺序很重要:首先,我们需要逃离\,然后$:

$r = '$1\1';
echo preg_replace('~(B.*?S)~', str_replace(array('\\', '$'), array('\\\\', '\\$'), $r), "BOSS");

See IDEONE demo (in your code, preg_replace('/(BEGIN.*?END)/su', str_replace(array('\\', '$'), array('\\\\', '\\$'), $_POST['content']), $template); or use the function I added at the start of the post).

看到IDEONE演示(在代码中,preg_replace(' /(结束开始。* ?)/苏”,str_replace(数组(' \ \ ',' $ '),数组(“\ \ \ \”、\ \ $),$ _POST['内容']),美元模板);或者使用我在文章开头添加的函数)。

#1


3  

In short, use this function to quote a dynamic replacement pattern:

简而言之,使用此函数引用动态替换模式:

function preg_quote_replacement($repl_str) {
    return str_replace(array('\\', '$'), array('\\\\', '\\$'), $repl_str);
}

The thing is, you need to escape the backslash in the replacement pattern. See preg_replace docs:

问题是,您需要在替换模式中转义反斜杠。看到preg_replace文档:

To use backslash in replacement, it must be doubled ("\\\\" PHP string).

要使用反斜杠替换,它必须加倍(“\\\\ \\\”PHP字符串)。

It can be done with a mere str_replace function:

只需一个str_replace函数即可完成:

$repl = 'C:\Users\Admin\1';
$template = "BEGIN this was the original content of the template END";
echo preg_replace('/(BEGIN.*?END)/su', str_replace('\\', '\\\\', $repl), $template);

See IDEONE demo

看到IDEONE演示

However, NOTE that the $ symbol is also special in the replacement pattern. Thus, we also need to escape this symbol. The order of these prelimnary replacements matter: first, we need to escape the \, and then the $:

但是,请注意,$符号在替换模式中也是特殊的。因此,我们也需要逃离这个符号。这些预备的替换物的顺序很重要:首先,我们需要逃离\,然后$:

$r = '$1\1';
echo preg_replace('~(B.*?S)~', str_replace(array('\\', '$'), array('\\\\', '\\$'), $r), "BOSS");

See IDEONE demo (in your code, preg_replace('/(BEGIN.*?END)/su', str_replace(array('\\', '$'), array('\\\\', '\\$'), $_POST['content']), $template); or use the function I added at the start of the post).

看到IDEONE演示(在代码中,preg_replace(' /(结束开始。* ?)/苏”,str_replace(数组(' \ \ ',' $ '),数组(“\ \ \ \”、\ \ $),$ _POST['内容']),美元模板);或者使用我在文章开头添加的函数)。