In this article, you will learn about the preg_quote function in PHP. The preg_quote() function in PHP 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.
what is the syntax of the PREG_QUOTE function in php?
preg_quote(input, delimiter)
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 |
examples of the PREG_QUOTE function
Example 1. In this example, we 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.";
}
?>