You are here

public static function SearchApiSolrConnection::escape in Search API Solr 7

Escapes special characters from a Solr query.

A complete list of special characters in Solr queries can be viewed at http://lucene.apache.org/java/docs/queryparsersyntax.html#Escaping%20Spe...

Parameters

string $value: The string to escape.

int $version: An integer representing the major solr version.

Return value

string An escaped string suitable for passing to Solr.

Overrides SearchApiSolrConnectionInterface::escape

File

includes/solr_connection.inc, line 674

Class

SearchApiSolrConnection
Represents a Solr server resource.

Code

public static function escape($value, $version = 0) {
  $replacements = array();
  $specials = array(
    '+',
    '-',
    '&&',
    '||',
    '!',
    '(',
    ')',
    '{',
    '}',
    '[',
    ']',
    '^',
    '"',
    '~',
    '*',
    '?',
    ':',
    "\\",
    'AND',
    'OR',
    'NOT',
  );

  // Solr 4.x introduces regular expressions, making the slash also a special
  // character.
  if ($version >= 4) {
    $specials[] = '/';
  }
  foreach ($specials as $special) {
    $replacements[$special] = "\\{$special}";
  }
  return strtr($value, $replacements);
}