Re: RegEx and ?

From: Date: Wed, 24 Apr 2002 08:24:40 +0000
Subject: Re: RegEx and ?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-94327@lists.php.net to get a copy of this message
Hi Berber, you generally need to do some string replacements on the text you're using to put through the match before you actually do the match (and same for replacements). Just have a look at the pattern syntax in the manual for the characters you need to do the replacement on... e.g $string = "Will this work?"; $criteria = "work?"; $criteria = str_replace("?", "\?", $criteria); if (preg_match("/" . $criteria . /is", $string)) { echo "Bingo."; } I usually make a function that will replace all the special characters. The only one that stands out against the rest is the $, because escaped once in a pcre function means that it is a variable - you need to escape it twice. Here's some of what I tend to use (cut out a function - the rest of the function won't mean much to you ;)): // Clear up the special characters in the $criteria. $criteria = str_replace("^", "\^", $criteria); $criteria = str_replace("[", "\[", $criteria); $criteria = str_replace("]", "\]", $criteria); $criteria = str_replace("*", "\*", $criteria); $criteria = str_replace("+", "\+", $criteria); $criteria = str_replace("{", "\}", $criteria); $criteria = str_replace("}", "\}", $criteria); $criteria = str_replace("?", "\?", $criteria); $criteria = str_replace("|", "\|", $criteria); // The $ is a little tricky, since it is used for end of string / line, // and for use with variables when escaped. It therefore needs two // backslashes. $criteria = str_replace("$", "\\$", $criteria); // We're using ! as a delimiter, so that must be replaced, too. $criteria = str_replace("!", "\!", $criteria); There's probably bits missing from that, since that's from a script in development, but you get the idea. Hope it's of some help, James "Boaz Yahav" <berber@netvision.net.il> wrote in message news:BB2EF6AF14F0D24FB89955B4906717620A7651@ntx.forest.netvision.net.il... When i try to use preg_match on strings that have "?" inside them it seems to not work. Any ideas how to bypass this? thanks berber

« previous php.general (#94327) next »