note 89720 deleted from function.preg-quote by cmb

From: Date: Sat, 20 Aug 2016 12:43:57 +0000
Subject: note 89720 deleted from function.preg-quote by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-206341@lists.php.net to get a copy of this message
Note Submitter: frostschutz ---- I wanted to escape a string of characters so I could match them in [], i.e. [.,-!"§$%\\\[\]\^]. Unfortunately preg_quote does not escape the - character which has a special meaning in [], i.e. [a-z]. So I used this hack: make - the delimiter of the expression, i.e. preg_quote(userinput, "-") preg_replace("-[$userinput]-u", "", $str) Apparently using a special char as a delimiter of a regular expression disables this character, i.e. even if it's escaped it's not understood as special character for the expression anymore. so the pattern "-[a\\-z]-u" matches the characters a, - and z, and not abc...xyz. It would be nice if preg_quote also escaped characters that have special meanings even if they have this meaning only under certain conditions, such as inside [].

« previous php.notes (#206341) next »