preg_quote

Quote regular expression characters

The special regular expression characters are: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : - #

Note that / is not a special regular expression character.

Syntax

preg_quote ( string $str [, string $delimiter = NULL ] ) : string

Parameters

str

The input string.

delimiter

If the optional delimiter is specified, it will also be escaped. This is useful for escaping the delimiter that is required by the PCRE functions. The / is the most commonly used delimiter.

Return

Returns the quoted (escaped) string.

Examples

1 · str

<?

$str = "$40 for a g3/400";

$return = preg_quote($str);

echo $return;

?>
\$40 for a g3/400

2 · delimiter

<?

$str = "$40 for a g3/400";
$delimiter = "/";

$return = preg_quote($str, $delimiter);

echo $return;

?>
\$40 for a g3\/400

3 · Asterisk

<?

// preg_quote($str) keeps the asterisks from having special meaning to the regular expression

$str = "*very*";

$return = preg_quote($str);

$pattern = "#$return#";
$replacement = "<i>$str</i>";
$subject = "This book is *very* difficult to find.";

$preg_replace = preg_replace($pattern, $replacement, $subject);

echo $preg_replace;

?>
This book is <i>*very*</i> difficult to find.

preg_filter

preg_grep

preg_last_error

preg_match

preg_match_all

preg_replace

preg_replace_callback

preg_replace_callback_array

preg_split

PHP

Home Menu