PHP quotemeta() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, quotemeta() adds a backslash before special regular-expression-style characters in a string.

It escapes . \ + * ? [ ^ ] $ ( and ). Each of these gets a backslash in front.

Syntax

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

PHP
$safe = quotemeta("1+1=2");
CallWhat it does
quotemeta("1+1=2")Returns "1\+1=2".
quotemeta("Hello. (world)?")Returns "Hello\. \(world\)\?".
quotemeta("a|b{2}")Returns "a|b{2}". Those characters are not changed.

The escaped characters are . \ + * ? [ ^ ] $ ( and ). The pipe, braces, slash, and hyphen are not escaped.

Library declarations
PHP
quotemeta(string $string): string

Parameters

ParameterDescription
stringRequired. The string to escape.

Return Value

Return ValueDescription
stringThe string with meta characters escaped. An empty input returns an empty string.

PHP Examples

Example 1 — Escape Special Characters

Each special character gets a backslash in front.

PHP
<?php
echo quotemeta("1+1=2"), PHP_EOL;
echo quotemeta("Hello. (world)?"), PHP_EOL;

How It Works

  1. The plus sign is special. It gets a backslash.
  2. Dots and brackets too. . ( ) and ? are all escaped.
  3. Other characters stay. The = sign and letters are not changed.
  4. Read the output. Backslashes show what was escaped.

Example 2 — Characters It Does Not Escape

Some regex symbols are left alone, which can surprise you.

PHP
<?php
echo quotemeta("a|b{2}"), PHP_EOL;
echo "[" . quotemeta("") . "]", PHP_EOL;

How It Works

  1. Pipe and braces. They stay as they are.
  2. Empty string. The result is also empty.
  3. Know the limits. quotemeta is not a full regex escaper.
  4. Better choice. preg_quote covers more characters.

Example 3 — Match a Literal Dot

Without escaping, a dot matches any character. With quotemeta, it matches only a real dot.

PHP
<?php
$pattern = "/" . quotemeta("3.14") . "/";

echo preg_match($pattern, "3x14"), PHP_EOL;
echo preg_match($pattern, "pi = 3.14"), PHP_EOL;

How It Works

  1. Escape the dot. 3.14 becomes 3\.14.
  2. Build the pattern. Wrap it in slashes.
  3. Test 3x14. It does not match, so the result is 0.
  4. Test 3. 14. It matches, so the result is 1.

Frequently Asked Questions

quotemeta() puts a backslash before the characters . \ + * ? [ ^ ] $ ( and ) so they are treated as plain text.
string quotemeta(string $string). It takes one string and returns the escaped string.
preg_quote() escapes more characters and can also escape your pattern delimiter. It is the safer choice for regular expressions.
In PHP 8 it returns an empty string. Older versions returned false.
No. For quotes use addslashes() or a context-specific escape function.

Next: rtrim()

rtrim() removes whitespace, or other characters you choose, from the end of a string.

rtrim() 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