English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The preg_last_error function is used to escape regular expression characters.
string preg_quote ( string $str [, string $delimiter = NULL ] )
preg_quote() requires a parameter str and adds a backslash in front of each character in the regular expression syntax. This is usually used when you have some runtime string that needs to be matched as a regular expression.
Regular expression special characters include: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
Parameter Description:
$str: Input string.
$delimiter: If the optional parameter delimiter is specified, it will also be escaped. This is usually used to escape delimiters used by PCRE functions. / is the most general delimiter.
Returns the escaped string.
<?php
$keywords = '$40 for a g3/400';
$keywords = preg_quote($keywords,'/);
echo $keywords;
?>
The execution result escaped $ and / Special characters, as shown below:
Returns \$40 for a g3\/400
<?php
//In this example, preg_quote($word) is used to maintain the original meaning of the asterisk, so that it does not use the special semantics of regular expressions.
$textbody = "This book is *very* difficult to find.";
$word = "*very*";
$textbody = preg_replace ("/" . preg_quote($word) . "/"
"<i>" . $word . "</i>
$textbody);
echo $textbody;
?>
The execution result is shown as follows:
This book is <i>*very*</i> difficult to find.