PHP function to escape MySQL regexp syntax

后端 未结 5 1643
自闭症患者
自闭症患者 2020-12-20 12:39

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

Any ideas?

5条回答
  •  佛祖请我去吃肉
    2020-12-20 13:08

    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.)

提交回复
热议问题