Only the characters listed on this page need to be escaped in PHP regex matching/replacing.

While < and > can act as delimiter, it doesn't need to be escaped in the given example because you already have /(slash) acting as a delimiter.

Referring to the link in question

The preg_quote() function may be used to escape a string for injection into a pattern and its optional second parameter may be used to specify the delimiter to be escaped.

Answer from hjpotter92 on Stack Overflow
🌐
PHP
php.net › manual › en › function.preg-quote.php
PHP: preg_quote - Manual
To escape characters with special meaning, like: .-[]() and so on, use \Q and \E. For example: <?php echo ( preg_match('/^'.( $myvar = 'te.t' ).'$/i', 'test') ? 'match' : 'nomatch' ); ?> Will result in: match But: <?php echo ( preg_match('/^\Q'.( ...
🌐
MojoAuth
mojoauth.com › escaping › regex-escaping-in-php
Regex Escaping in PHP | Escaping Methods in Programming Languages
In this example, the regex pattern '/./' will successfully match the dot in example.com, thanks to the escape character. When working with regex escaping in PHP, following best practices can significantly reduce errors and improve code readability: Always Escape Special Characters: Ensure that ...
🌐
BrainBell
brainbell.com › php › escaping-special-characters-in-regular-expressions.html
Escaping special characters in regular expressions in PHP – BrainBell
Example: Using preg_quote · Escape the string with preg_quote() function in the pattern: <?php $string = 'Hi, 2+3 is equal to 5'; $find = preg_quote('2+3'); $pattern = '/'.$find.'/'; $found = preg_match($pattern, $string, $match);# true print_r($match); // 2+3 · The preg_quote() function is particularly useful when you dynamically insert a string into your regex pattern which may contain special characters that need escaping.
🌐
Designcise
designcise.com › web › tutorial › how-to-escape-regular-expression-special-characters-in-php
How to Escape Regex Special Chars in PHP? - Designcise
June 4, 2021 - You can use the backslash character (i.e. \) to escape the regular expression special characters. For example: $str = 'hello?'; $replaceWith = 'hey?'; $pattern = '/hello\?/'; echo preg_replace($pattern, $replaceWith, $str); // 'hey?'
Top answer
1 of 2
280

preg_quote() is what you are looking for:

Description

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

preg_quote() takes str and puts a backslash in front of every character that is part of the regular expression syntax. This is useful if you have a run-time string that you need to match in some text and the string may contain special regex characters.

The special regular expression characters are: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -

Parameters

str

The input string.

delimiter

If the optional delimiter is specified, it will also be escaped. This is useful for escaping the delimiter that is required by the PCRE functions. The / is the most commonly used delimiter.

Importantly, note that if the $delimiter argument is not specified, the delimiter - the character used to enclose your regex, commonly a forward slash (/) - will not be escaped. You will usually want to pass whatever delimiter you are using with your regex as the $delimiter argument.

Example - using preg_match to find occurrences of a given URL surrounded by whitespace:

$url = 'http://stackoverflow.com/questions?sort=newest';

// preg_quote escapes the dot, question mark and equals sign in the URL (by
// default) as well as all the forward slashes (because we pass '/' as the
// $delimiter argument).
$escapedUrl = preg_quote($url, '/');

// We enclose our regex in '/' characters here - the same delimiter we passed
// to preg_quote
$regex = '/\s' . $escapedUrl . '\s/';
// $regex is now:  /\shttp\:\/\/stackoverflow\.com\/questions\?sort\=newest\s/

$haystack = "Bla bla http://stackoverflow.com/questions?sort=newest bla bla";
preg_match($regex, $haystack, $matches);

var_dump($matches);
// array(1) {
//   [0]=>
//   string(48) " http://stackoverflow.com/questions?sort=newest "
// }
2 of 2
2

It would be much safer to use Prepared Patterns from T-Regx (i'm the author of the library):

$url = 'http://stackoverflow.com/questions?sort=newest';

$pattern = Pattern::inject('\s@\s', [$url]);
                                    // ↑ $url is quoted

then perform normal match:

$haystack = "Bla bla http://stackoverflow.com/questions?sort=newest bla bla";

$matcher = pattern->match($haystack);
foreach ($matcher as $match) {
}

you can even use it with preg_match():

preg_match($pattern, 'foo', $matches);
🌐
SSOJet
ssojet.com › escaping › regex-escaping-in-php
Regex Escaping in PHP | Escaping Techniques in Programming
Characters such as ., *, +, ?, ^, $, (, ), [, ], {, }, |, and \ all act as metacharacters. To match these characters literally within your PHP regex pattern, you must escape them by preceding them with a backslash (\).
🌐
DocStore
docstore.mik.ua › orelly › webprog › pcook › ch13_09.htm
Escaping Special Characters in a Regular Expression (PHP Cookbook)
You want to have characters such as * or + treated as literals, not as metacharacters, inside a regular expression. This is useful when allowing users to type in search strings you want to use inside a regular expression · Use preg_quote( ) to escape Perl-compatible regular-expression ...
🌐
Code.mu
code.mu › en › php › book › prime › regular › escaping-special-characters
Escaping Special Characters in PHP Regex
In the following example, the regex author wanted the search pattern to look like this: the letter 'a', then a plus '+', then the letter 'x'. However, the code author did not escape the '+' character, so the search pattern actually looks like this: the letter 'a' one or more times, then the ...
Find elsewhere
🌐
SitePoint
sitepoint.com › php
Regex for Special Characters - PHP - SitePoint Forums | Web Development & Design Community
July 5, 2012 - I have some questions about the Regex below… // Check for Special-Character. if (empty($errors)){ if (!preg_match("#[\\~\\`\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\_\\-\\+\\=\\{\\}\\[\\]\\|\\:\\;\\&lt;\\&gt;\\.\\?\\/\\\\\\\…
🌐
GeeksforGeeks
geeksforgeeks.org › php › function-to-escape-regex-patterns-before-applied-in-php
Function to escape regex patterns before applied in PHP - GeeksforGeeks
July 12, 2025 - <?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; ?> ... 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.
🌐
O'Reilly
oreilly.com › library › view › php-cookbook › 1565926811 › ch13s09.html
13.8. Escaping Special Characters in a Regular Expression - PHP Cookbook [Book]
November 20, 2002 - These functions escape the metacharacters with backslash. The quotemeta( ) function doesn’t match all POSIX metacharacters. The characters {, }, and | are also valid metacharacters but aren’t converted.
Authors: David SklarAdam Trachtenberg
Published: 2002
Pages: 640
🌐
Regular-Expressions.info
regular-expressions.info › php.html
Using Regular Expressions with PHP
When forward slashes are used as the regex delimiter, any forward slashes in the regular expression have to be escaped with a backslash. So https://www\.regexp\.info/ becomes '/https:\/\/www\.regexp\.info\//'. Just like Perl, the preg functions allow any non-alphanumeric character ...
🌐
Code.mu
code.mu › en › php › book › prime › regular › special-characters-list
List of Special Characters in Regex in PHP
Are not special characters: @ : , ' " ; - _ = < > % # ~ ` & ! ... Write a regex that will find the string 'a.a', without capturing the others. ... Write a regex that will find the string '2+3', without capturing the others. ... Write a regex that will find the strings '2+3', '2++3', '2+++3', ...
🌐
O'Reilly
oreilly.com › library › view › php-in-a › 0596100671 › ch15s03.html
15.3. Regexp Special Characters - PHP in a Nutshell [Book]
October 13, 2005 - For example: preg_match("/[A-Za-z ]*/", $string); // matches "", "a", "aaaa", "The sun has got his hat on", etc preg_match("/-?[0-9]+/", $string); // matches 1, 100, 324343995, and also -1, -234011, etc.
Author: Paul Hudson
Published: 2005
Pages: 372
🌐
GitHub
github.com › Hamz-a › php-regex-best-practices › blob › master › 06 Escaping a backslash hell.md
php-regex-best-practices/06 Escaping a backslash hell.md at master · Hamz-a/php-regex-best-practices
$regex = <<<'regex' ~ fancy # fancy's explanation regex # note ^ no escape needed ~x regex; preg_match_all($regex, $input,$m); We used the nowdoc string syntax but we could also have used a heredoc. Read the difference from the manual. Don't forget that double quoted strings have a special power in php which means it might interfer with the regex. Also consider another factor which is consistency. A lot of times, I see characters being escaped which are not required to escape.
Author: Hamz-a
🌐
W3Schools
w3schools.com › php › php_string_escape.asp
PHP - Escape Characters
In PHP, an escape character is a backslash \ followed by the character you want to insert. An example of an illegal character is a double quote inside a string that is surrounded by double quotes: