I found the following function in the Sphinx PHP API code:
function sphinxapi_EscapeString($string)
{
$from = ['\\', '(', ')', '|', '-', '!', '@', '~', '"', '&', '/', '^', '$', '=', '<'];
$to = ['\\\\', '\(', '\)', '\|', '\-', '\!', '\@', '\~', '\"', '\&', '\/', '\^', '\$', '\=', '\<'];
return str_replace($from, $to, $string);
}
However, it doesn't seem to work properly because when I use strings with certain characters in them in queries Sphinx throws exceptions.
An example is the quote character "
. EscapeString()
puts a backslash \
in front of it, but Sphinx throws an exception saying:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 index my_index: syntax error, unexpected $end near ''' in ..
If I add two more backslashes, making it \\\"
, then no error is thrown.
What's the deal here? Why isn't EscapeString()
working?