How to quote replacement in preg_replace?

Tamás Bolvári

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.

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

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:

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

It can be done with a mere str_replace function:

$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

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).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP preg_replace: Case insensitive match with case sensitive replacement

From Dev

conditional preg_replace for optional backreference replacement

From Dev

preg_replace to preg_replace_callback with an array as replacement

From Dev

preg_replace use matched pattern in replacement

From Dev

How to preg_replace pattern on string

From Dev

preg_replace() with variable in replacement string eats up first character

From Dev

How to use preg_replace to replace links and image links

From Dev

How to preg_replace repeating words

From Dev

How to preg_replace html tags?

From Dev

PHPExcel - How to replace text using preg_replace

From Dev

how to implement template with conditions with preg_replace

From Dev

how to use preg_replace with dimensional pattern and replacement in php?

From Dev

Get dynamic replacement with preg_replace()

From Dev

PHP keyword replacement using preg_replace and a FOR loop

From Dev

preg_replace pattern and replacement for image tag of mediawiki markup

From Dev

preg_replace The /e modifier is deprecated using arrays as pattern and replacement

From Dev

preg_replace : how get what is replaced

From Dev

How to disable "smart" quote replacement for Russian in MS Word?

From Dev

preg_replace to preg_replace_callback with an array as replacement

From Dev

How to preg_replace repeating words

From Dev

How can I get this preg_replace replacement?

From Dev

proper replacement for Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback?

From Dev

how to use preg_replace with dimensional pattern and replacement in php?

From Dev

Get dynamic replacement with preg_replace()

From Dev

Understanding how preg_replace function works

From Dev

how to use preg_replace() in php?

From Dev

How to use variables in preg_replace in pattern?

From Dev

How do I tell GNU Parallel to not quote the replacement string

From Dev

How to replace quote ( `”` ) with normal quote (`"`)

Related Related

  1. 1

    PHP preg_replace: Case insensitive match with case sensitive replacement

  2. 2

    conditional preg_replace for optional backreference replacement

  3. 3

    preg_replace to preg_replace_callback with an array as replacement

  4. 4

    preg_replace use matched pattern in replacement

  5. 5

    How to preg_replace pattern on string

  6. 6

    preg_replace() with variable in replacement string eats up first character

  7. 7

    How to use preg_replace to replace links and image links

  8. 8

    How to preg_replace repeating words

  9. 9

    How to preg_replace html tags?

  10. 10

    PHPExcel - How to replace text using preg_replace

  11. 11

    how to implement template with conditions with preg_replace

  12. 12

    how to use preg_replace with dimensional pattern and replacement in php?

  13. 13

    Get dynamic replacement with preg_replace()

  14. 14

    PHP keyword replacement using preg_replace and a FOR loop

  15. 15

    preg_replace pattern and replacement for image tag of mediawiki markup

  16. 16

    preg_replace The /e modifier is deprecated using arrays as pattern and replacement

  17. 17

    preg_replace : how get what is replaced

  18. 18

    How to disable "smart" quote replacement for Russian in MS Word?

  19. 19

    preg_replace to preg_replace_callback with an array as replacement

  20. 20

    How to preg_replace repeating words

  21. 21

    How can I get this preg_replace replacement?

  22. 22

    proper replacement for Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback?

  23. 23

    how to use preg_replace with dimensional pattern and replacement in php?

  24. 24

    Get dynamic replacement with preg_replace()

  25. 25

    Understanding how preg_replace function works

  26. 26

    how to use preg_replace() in php?

  27. 27

    How to use variables in preg_replace in pattern?

  28. 28

    How do I tell GNU Parallel to not quote the replacement string

  29. 29

    How to replace quote ( `”` ) with normal quote (`"`)

HotTag

Archive