Function to escape regex patterns before applied in PHP

Use preg_quote() function in PHP to escape regex patterns before it is applied in run time. The preg_quote() function puts a backslash in front of every character within the specified string that would be a part of the regex syntax in PHP, thereby making them escape sequences. These characters include:
. \ + * ? [ ^ ] $ ( ) { } = ! | : – #

Syntax:

string preg_quote( string $str, string $delimiter = NULL )

Parameters:

  • $str: This parameter holds the input string.
  • $delimiter: It is optional parameter. The most common delimiter is ‘/’ as it is not a special regex character that would be normally handled by preg_quote(). Primarily used in conjunction with preg_replace() function.

Returns: It returns the delimiter containing string.

Below programs illustrates the preg_quote() function.



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a string which need to be escaped
$str = "Welcome to GfG! (+ The course fee. $400) /";
      
echo "Before Processing - " . $str . PHP_EOL;
  
// Use preg_quote() on above string
$processedStr = preg_quote($str);
  
// Display the output
echo "After Processing - " . $processedStr;
  
?>

chevron_right


Output:

Before Processing - Welcome to GfG! (+ The course fee. $400) /
After Processing - Welcome to GfG\! \(\+ The course fee\. \$400\) /

Notice that a backslash was put in front of every special character, but not for the forward slash. For that, we can use the delimiter, see the program below:

Program 2

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a string which need to be escaped
$str = "Welcome to GfG! (+ The course fee. $400) /";
      
echo "Before Processing - " . $str . PHP_EOL;
  
// Use preg_quote() on above string
$processedString = preg_quote($str, '/');
  
// Display the output
echo "After Processing - " . $processedString;
  
?>

chevron_right


Output:

Before Processing - Welcome to GfG! (+ The course fee. $400) /
After Processing - Welcome to GfG\! \(\+ The course fee\. \$400\) \/

Below program demonstrates the combined use of preg_quote() and preg_replace() function.

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP Program emphasize the word within * * and 
// use font style to italic within [ ] using
// both preg_quote() and preg_replace() function
  
$str = "The *article* was written by [GFG]";
      
// The words to be formatted
$word1 = "*article*";
$word2 = "[GFG]";
      
// The first string only applies bold to word 1,
// preg_quote() escapes the * *
$processedStr1 = preg_replace("/" . preg_quote($word1, '/')
            . "/", "<strong>" . $word1 . "</strong>", $str);
  
echo "BOLD ONLY - " . $processedStr1 . PHP_EOL;
  
// The second string only applies italics to 
// word 2, preg_quote() escapes the [ ]
$processedStr2 = preg_replace("/" . preg_quote($word2, '/')
            . "/", "<em>" . $word2 . "</em>", $str);
  
echo "ITALIC ONLY - " . $processedStr2 . PHP_EOL;
  
// Combining both text formattings and display
$bothReplacementsCombined = preg_replace("/" .
            preg_quote($word2, '/') . "/",
            "<em>" . $word2 . "</em>", $processedStr1);
  
echo "BOTH COMBINED - " . $bothReplacementsCombined;
  
?>

chevron_right


Output:

BOLD ONLY - The *article* was written by [GFG]
ITALIC ONLY - The *article* was written by [GFG]
BOTH COMBINED - The *article* was written by [GFG]

Note: To notice the application of the text formatting tags, you should run PHP on your local server and echo to the browser.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.