Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

preg_quote

Description

Quote regular expression characters

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

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.
HomeMenu