Skip to content

Commit

Permalink
escape / with \\
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Laulhau committed Apr 21, 2014
1 parent 99fbfbf commit 343d726
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/Elastica/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ public static function escapeTerm($term)
{
$result = $term;
// \ escaping has to be first, otherwise escaped later once again
$chars = array('\\', '+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '/');
$chars = array('\\', '+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':');

foreach ($chars as $char) {
$result = str_replace($char, '\\' . $char, $result);
}

// since elastisearch uses lucene 4.0 / needs to be escaped by \\
$result = str_replace('/', '\\\\/', $result);

return $result;
}

Expand Down
2 changes: 1 addition & 1 deletion test/lib/Elastica/Test/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getEscapeTermPairs()
public function testEscapeTermSpecialCharacters()
{
$before = '\\+-&&||!(){}[]^"~*?:/';
$after = '\\\\\\+\\-\\&&\\||\\!\\(\\)\\{\\}\\[\\]\\^\\"\\~\\*\\?\\:\\/';
$after = '\\\\\\+\\-\\&&\\||\\!\\(\\)\\{\\}\\[\\]\\^\\"\\~\\*\\?\\:\\\\/';

$this->assertEquals(Util::escapeTerm($before), $after);
}
Expand Down

0 comments on commit 343d726

Please sign in to comment.