Use preg_quote() to safely use special characters in a regular expression:
<?php
$search = preg_quote("://", "/");
$input = 'https://www.91xjr.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 »
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.
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 |
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. |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!