I'm looking for something similar to preg_quote, but for the MySQL regexp syntax.
Any ideas?
I'm looking for something similar to preg_quote, but for the MySQL regexp syntax.
Any ideas?
Thank @bobince's good answer. But it has a problem if you need to use mysql_real_escape_string after quoting, that I mentioned it in a comment.
Actually preg_quote and mysql_real_escape_string have overlap and it makes this problem! mysql_real_escape_string must not escape \ in this case. So I suggest:
function regexpEscape(/*string*/ $input) { // Uncomment `string` for PHP 7.0+
return addcslashes(preg_quote($input), "\0'\"\n\r\x1A"); // charlist = All characters that escape by real_escape_string except backslash
}
(For charlist see: http://php.net/manual/en/mysqli.real-escape-string.php)
I know this is not an ideal way, but couldn't find a better way.