PHP preg_quote() Function

❮ PHP RegExp Reference

Example

Use preg_quote() to safely use special characters in a regular expression:

<?php
$search = preg_quote("://", "/");
$input = 'https://www.w3schools.com/';
$pattern = "/$search/";
if(preg_match($pattern, $input)) {
  echo "The input is a URL.";
} else {
  echo "The input is not a URL.";
}
?>
Try it Yourself »

Definition and Usage

The preg_quote() function adds a backslash to characters that have a special meaning in regular expressions so that searches for the literal characters can be done. This function is useful when using user input in regular expressions.


Syntax

preg_quote(input, delimiter)

Parameter Values

Parameter Description
input Required. The string to be escaped
delimiter Optional. Defaults to null. This parameter expects a single character indicating which delimiter the regular expression will use. When provided, instances of this character in the input string will also be escaped with a backslash

Technical Details

Return Value: Returns a string with all of the special characters escaped with a backslash
PHP Version: 4+
Changelog: PHP 7.3 - The # character is now considered a special character and will be escaped.

PHP 5.3 - The - character is now considered a special character and will be escaped.

❮ PHP RegExp Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.