PHP Modules Tutorials - Herong's Tutorial Examples - v5.18, by Herong Yang
preg_quote() - Escape Characters in Pattern
This section provides a tutorial example on how to quote special characters in a string to be used as a regular expression pattern using the preg_quote() function.
Regular expression pattern uses a set of reserved characters, ". \ + * ? [ ^ ] $ ( ) { } = ! < > | : - #", to provide special functionalities. If you want to match any one of them in the subject string, you need to escape it using a backslash. This is called quoting a special character. For example, "\." is a quoted special character that matches a single ".". "\\" is a quoted special character that matches a single "\".
If you want to use the string stored in a variable as the search pattern, you should call the preg_quote() function to quote any special characters in the string:
$quoted = preg_quote($pattern, $delimiter); where: $pattern: String to be quoted $delimiter: Delimiter character to be quoted as special character $quoted: Quoted string
Here is an example script that shows the difference between un-quoted and quoted strings:
<?php
# preg-quote-escape-character.php
# Copyright 2009-2024 (c) HerongYang.com. All Rights Reserved.
$url = "https://herongyang.com/PHP/index.html";
$path = "/PHP/index.html";
print("URL: $url\n");
print("Path: $path\n");
print("Remove path as unquoted pattern:\n");
$removed = preg_replace("/$path/", "", $url);
print(" URL without path: $removed\n");
print("Remove path as quoted pattern:\n");
$quoted = preg_quote($path, "/");
$removed = preg_replace("/$quoted/", "", $url);
print(" Quoted path: $quoted\n");
print(" URL without path: $removed\n");
?>
If you run the above script, you will see the following output:
herong$ php preg-quote-escape-character.php URL: https://herongyang.com/PHP/index.html Path: /PHP/index.html Remove path as unquoted pattern: PHP Warning: preg_replace(): Unknown modifier 'P' in /Users/herong/preg-quote-escape-character.php on line 12 URL without path: Remove path as quoted pattern: Quoted path: \/PHP\/index\.html URL without path: https://herongyang.com
As you can see from the output, using the un-quoted string as the search pattern failed in the preg_match() call, because the first un-quoted character '/' inside the pattern string is considered as the ending delimiter. The "P" character after "/" is considered as a modifier and is invalid.
Table of Contents
Introduction and Installation of PHP
Managing PHP Engine and Modules on macOS
Managing PHP Engine and Modules on CentOS
DOM Module - Parsing HTML Documents
GD Module - Manipulating Images and Pictures
MySQLi Module - Accessing MySQL Server
OpenSSL Module - Cryptography and SSL/TLS Toolkit
►PCRE Module - Perl Compatible Regular Expressions
preg_match() - Regular Expression Match
preg_grep() - Search Array Members
preg_replace() - Replace Matched Substring
preg_split() - Split String into Tokens
►preg_quote() - Escape Characters in Pattern
SOAP Module - Creating and Calling Web Services
SOAP Module - Server Functions and Examples