Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Échapper une regex

<?php
$regex = 'test.';

// match
print preg_match('/^'.$regex.'$/i', 'tests') ? 'match' : 'nomatch';
print PHP_EOL;

// nomatch
print preg_match('/^'.preg_quote($regex).'$/i', 'tests') ? 'match' : 'nomatch';
print PHP_EOL;

// nomatch
print preg_match('/^\Q'.$regex.'\E$/i', 'tests') ? 'match' : 'nomatch';
print PHP_EOL;
?>

Pour utiliser des caractères littéraux dans une regex, il est possible d’utiliser preg_quote() : elle ajoute un antislash avant chaque caractère spécial de la chaîne. N’oubliez pas d’utiliser le second argument, qui peut contenir n’importe quel délimiteur arbitraire.

Une autre option est d’utiliser les caractères \Q et \E dans la regex : ils transforment tous les caractères spéciaux en valeurs littérales.

See Also

PHP Features

Last updated: 11 October 2026