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,


"Boaz Yahav" <[EMAIL PROTECTED]> wrote in message
When i try to use preg_match on strings that have "?" inside them it
seems to not work.
Any ideas how to bypass this?



PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to