PHP preg_quote() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Regex

Definition

In PHP, preg_quote() escapes special regular-expression characters so a string can be used safely as literal text inside a pattern.

Characters such as . \ + * ? [ ^ ] $ ( ) { } = ! < > | : - and # are escaped with a backslash. If you pass a delimiter, that character is escaped too.

Syntax

Call preg_quote() with the values you need. Start with this line.

PHP
$safe = preg_quote($userText, '/');
CallWhat it does
preg_quote('price $5.00', '/')Returns "price \$5\.00" so $ and . are literal.
preg_quote('a/b', '/')Returns "a\/b" because the delimiter / is escaped.
preg_quote('hello')Returns "hello" unchanged when there are no special characters.

Use preg_quote() whenever part of a pattern comes from a variable, a file name, or user input. Then wrap the result in your delimiters: '/' . preg_quote($x, '/') . '/'.

Library declarations
PHP
preg_quote(string $str, ?string $delimiter = null): string

Parameters

ParameterDescription
strRequired. The string to escape for safe use inside a PCRE pattern.
delimiterOptional. If set, that delimiter character is escaped as well. Pass the same delimiter you use around the pattern.

Return Value

Return ValueDescription
stringA copy of the string with regex special characters escaped.

PHP Examples

Example 1 — Escape Money Text for a Pattern

Turn a price string into a safe literal piece of a regex.

PHP
<?php
$literal = preg_quote('price $5.00', '/');
echo $literal, PHP_EOL;

$pattern = '/' . $literal . '/';
echo preg_match($pattern, 'The price $5.00 is OK'), PHP_EOL;

How It Works

  1. The raw text contains $ and . , which are special in regex.
  2. preg_quote() escapes them so they mean “dollar” and “dot”.
  3. The escaped text is wrapped in / delimiters to build a full pattern.
  4. preg_match() finds the exact phrase in the sentence and returns 1.

Example 2 — Escape the Delimiter Too

Show why passing the delimiter matters when the text contains that character.

PHP
<?php
echo preg_quote('a/b', '/'), PHP_EOL;
echo preg_quote('a/b'), PHP_EOL;

How It Works

  1. With delimiter '/', the slash inside a/b is escaped as \/.
  2. Without a delimiter argument, the slash is left alone.
  3. If you build /. ../ patterns, always pass '/' as the second argument.
  4. Otherwise an unescaped / would end the pattern too early.

Example 3 — Build a Search Pattern from User Input

Safely look for a user needle as literal text inside a larger string.

PHP
<?php
$needle = 'file.txt';
$pattern = '/' . preg_quote($needle, '/') . '/i';
echo preg_match($pattern, 'See FILE.TXT now'), PHP_EOL;

How It Works

  1. The needle file. txt contains a dot, which normally means “any character”.
  2. preg_quote() turns it into a literal dot.
  3. The i flag makes the search case insensitive.
  4. FILE. TXT matches the escaped needle, so the result is 1.

Frequently Asked Questions

It escapes special regex characters in a string so the text is treated as literal when used inside a PCRE pattern.
So the delimiter character itself is escaped. If you wrap patterns in /, pass '/' as the second argument.
It is required for literal user text inside patterns, but you must still avoid dangerous pattern structures and ReDoS. Prefer fixed patterns when you can.
No. It only escapes the string. You still add delimiters (and flags) yourself when you build the full pattern.
addslashes() is not for regex. preg_quote() knows which characters are special to PCRE and escapes those.

Next: preg_replace()

preg_replace() replaces every match of a regular expression with a replacement string (or .

preg_replace() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful