PHP preg_quote() Functie
Voorbeeld
Gebruik preg_quote() om veilig speciale tekens in een reguliere expressie te gebruiken:
<?php
$search = preg_quote("://", "/");
$input = 'https://www.w3schools.com/';
$pattern = "/$search/";
if(preg_match($pattern, $input)) {
echo
"The input is a URL.";
} else {
echo "The input is not a URL.";
}
?>
Definitie en gebruik
De preg_quote()
functie voegt een backslash toe aan tekens die een speciale betekenis hebben in reguliere expressies, zodat gezocht kan worden naar de letterlijke tekens. Deze functie is handig bij het gebruik van gebruikersinvoer in reguliere expressies.
Syntaxis
preg_quote(input, delimiter)
Parameterwaarden
Parameter | Description |
---|---|
input | Required. The string to be escaped |
delimiter | Optional. Defaults to null. This parameter expects a single character indicating which delimiter the regular expression will use. When provided, instances of this character in the input string will also be escaped with a backslash |
Technische details
Winstwaarde: | Retourneert een tekenreeks waarbij alle speciale tekens zijn geëscaped met een backslash |
---|---|
PHP-versie: | 4+ |
Wijzigingslogboek: | PHP 7.3 - Het #-teken wordt nu als een speciaal teken beschouwd en zal worden geëscaped. PHP 5.3 - Het - teken wordt nu beschouwd als een speciaal teken en zal worden geëscaped. |
❮ PHP RegExp-referentie