CKTIS / PHP
preg_quote()
Escape regular-expression metacharacters in a literal fragment before inserting it into a pattern.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
preg_quote(string $str, ?string $delimiter = null): stringEscape regular-expression metacharacters in a literal fragment before inserting it into a pattern. Supply the enclosing delimiter separately so occurrences of it are escaped as well. The output is for a pattern, not for preg_replace replacement text.
Signature
preg_quote(string $str, ?string $delimiter = null): string
Example output
[1,0]
Examples
<?php
$literal="A+B/2"; $pattern="/^".preg_quote($literal,"/")."$/D";
echo json_encode([preg_match($pattern,"A+B/2"),preg_match($pattern,"AAAB/2")]);
Result
[1,0]