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
Since many regexes are surrounded by forward slashes, if you have one in your regex as text you must escape it yourself otherwise it'll terminat the regex. up · down · 4 rwillmann at crooce dot com ¶ · 9 years ago · List of specials is incomplete: --- sample code --- $specials = '.\+*?[^]$(){}=!<>|:-'; for ($i = 0; $i <= 255; $i++) { if (chr($i) !== preg_quote(chr($i))) { printf("Character 0xx quoted%s\n", $i, (strpos($specials, chr($i)) === FALSE) ?
🌐
BrainBell
brainbell.com › php › escaping-special-characters-in-regular-expressions.html
Escaping special characters in regular expressions in PHP – BrainBell
Escaping the special meaning of a character is done with the backslash character as with the expression "2\+3", which matches the string "2+3". If the + isn’t escaped, the pattern matches one or many occurrences of the character 2 followed by the character 3. ... <?php $string = 'Hi, 2+3 ...
🌐
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);
🌐
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;\\.\\?\\/\\\\\\\…
🌐
Code.mu
code.mu › en › php › book › prime › regular › special-characters-list
List of Special Characters in Regex in PHP
Often there is doubt whether a given character is special. Some go so far as to escape all suspicious characters in a row. However, this is bad practice (clutters the regex with backslashes).
🌐
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 ...
🌐
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 any special characters that should be treated literally are escaped.
Find elsewhere
🌐
SSOJet
ssojet.com › escaping › regex-escaping-in-php
Regex Escaping in PHP | Escaping Techniques in Programming
PHP's preg_quote() function is designed for this exact purpose. It automatically escapes any characters within a string that have special meaning in regular expressions, ensuring they're treated as literal characters.
🌐
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 - 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.
🌐
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 - 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 metacharacters:
Authors: David SklarAdam Trachtenberg
Published: 2002
Pages: 640
🌐
Code.mu
code.mu › en › php › book › prime › regular › escaping-special-characters
Escaping Special Characters in PHP Regex
Suppose we want a special character to represent itself. To do this, it must be escaped with a backslash. Let's look at some examples. 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 ...
🌐
Designcise
designcise.com › web › tutorial › which-regular-expression-characters-need-escaping-in-php
Which RegExp Chars Need Escaping in PHP? - Designcise
June 3, 2021 - In a PHP regular expression pattern, the following meta-characters (i.e. characters that have a special meaning in a regular expression pattern) must be escaped wherever in the pattern they appear (except within square brackets): \ ^ $ . [ ] | ...
🌐
NTU Singapore
www3.ntu.edu.sg › home › ehchua › programming › howto › Regexe.html
Regular Expression (Regex) Tutorial
Most of the special regex characters lose their meaning inside bracket list, and can be used as they are; except ^, -, ] or \. To include a ], place it first in the list, or use escape \].
🌐
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 - As mentioned before, $ is a regexp symbol in its own right; however, here we precede it with a backslash, which works as an escape character, turning the $ into a standard character and not a regexp symbol. We match precisely one symbol from the range A-Z, a-z, and _, then match zero or more symbols from the range A-Z, a-z, underscore, and 0-9. If you're able to parse this in your head, you will see that this regexp will match PHP variable names: preg_match("/\$[A-Za-z_][A-Za-z_0-9]*/", $string); Table 15-3 shows a list of regular expressions using +, *, and ?, and whether or not a match is made.
Author: Paul Hudson
Published: 2005
Pages: 372