views:

36

answers:

2

I'm looking for something similar to preg_quote, but for the MySQL regexp syntax.

Any ideas?

+1  A: 

There's no native MySQL function for that. You might just need to use preg_quote before passing the regular expression to the MySQL query.

Ruel
+1  A: 

MySQL regexps are the ‘extended’ POSIX variant (ERE), available in PHP as the deprecated ereg_ functions.

Unfortunately there is no ereg_quote in PHP, however PCRE's special characters are a superset of ERE's special characters, and backslash-escaping a non-special punctuation character doesn't harm it, so you can get away with using preg_quote safely.

(Naturally you will need parameterised queries or mysql_real_escape_string after that quoting, to stop the backslashes being misinterpreted as MySQL's non-ANSI-standard string literal escapes.)

bobince
Slight caveat: the null control character. `preg_quote` escapes it to `\000`, which is a format MySQL doesn't recognise. But then MySQL's regex engine can't cope with null characters anyway, it takes them as terminators.
bobince