Sphinx\SphinxClient::escapeString PHP Method

escapeString() public method

Escapes characters that are treated as special operators by the query language parser
public escapeString ( string $string ) : string
$string string unescaped string
return string Escaped string.
    public function escapeString($string)
    {
        $from = array('\\', '(', ')', '|', '-', '!', '@', '~', '"', '&', '/', '^', '$', '=');
        $to = array('\\\\', '\\(', '\\)', '\\|', '\\-', '\\!', '\\@', '\\~', '\\"', '\\&', '\\/', '\\^', '\\$', '\\=');
        return str_replace($from, $to, $string);
    }

Usage Example

 /**
  * Search for a query string
  * @param  string  $query   Search query
  * @param  array   $indexes Index list to perform the search
  * @param  boolean $escape  Should the query to be escaped?
  *
  * @return array           Search results
  *
  * @throws EmptyIndexException If $indexes is empty
  * @throws \RuntimeException If seaarch failed
  */
 public function search($query, array $indexes, $escape = true)
 {
     if (empty($indexes)) {
         throw new EmptyIndexException('Try to search with empty indexes');
     }
     if ($escape) {
         $query = $this->sphinx->escapeString($query);
     }
     $qIndex = implode(' ', $indexes);
     $results = $this->sphinx->query($query, $qIndex);
     if (!is_array($results)) {
         throw new \RuntimeException(sprintf('Searching for "%s" failed. Result is not an array. Error "%s"', $query, $this->sphinx->getLastError()));
     }
     if (!isset($results['status'])) {
         throw new \RuntimeException(sprintf('Searching for "%s" failed. Result with no status. Error "%s"', $query, $this->sphinx->getLastError()));
     }
     if ($results['status'] !== SphinxClient::SEARCHD_OK && $results['status'] !== SphinxClient::SEARCHD_WARNING) {
         throw new \RuntimeException(sprintf('Searching for "%s" failed. Result has bad status. Error "%s"', $query, $this->sphinx->getLastError()));
     }
     return $results;
 }
All Usage Examples Of Sphinx\SphinxClient::escapeString