Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape elasticsearch special characters in PHP

I want to create a function that escapes elasticsearch special characters by adding a \ before the characters in PHP. The special chars that Elasticsearch use are: + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /

I'm not very familliar with regex, but I have found a piece of code that simply removes the special chars, but i prefer to escape them, because the might be relevant. The code i use:

$s_input = 'The next chars should be escaped: + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ / Did it work?';
$search_query = preg_replace('/(\+|\-|\=|\&|\||\!|\(|\)|\{|\}|\[|\]|\^|\"|\~|\*|\<|\>|\?|\:|\\\\)/', '', $s_input);

This outputs:

The next chars should be escaped / Did it work

So there are two problems: this code removes the special chars, while i want to escape them with a \. Furthermore: this code doesn't escape the \. Does anyone know how to escape the Elasticsearch special characters?

like image 745
wjhulzebosch Avatar asked Nov 21 '15 15:11

wjhulzebosch


Video Answer


1 Answers

You can use preg_match with backreferences as stribizhev has noticed it (simpliest way) :

$string = "The next chars should be escaped: + - = && || > < ! ( ) { } [ ] ^ \" ~ * ? : \ / Did it work?"; 

function escapeElasticReservedChars($string) {
    $regex = "/[\\+\\-\\=\\&\\|\\!\\(\\)\\{\\}\\[\\]\\^\\\"\\~\\*\\<\\>\\?\\:\\\\\\/]/";
    return preg_replace($regex, addslashes('\\$0'), $string);
}
echo escapeElasticReservedChars($string);

or use preg_match_callback function to achieve that. Thank to the callback, you will be able to have the current match and edit it.

A callback that will be called and passed an array of matched elements in the subject string. The callback should return the replacement string. This is the callback signature:

Here is in action :

<?php 
$string = "The next chars should be escaped: + - = && || > < ! ( ) { } [ ] ^ \" ~ * ? : \ / Did it work?"; 

function escapeElasticSearchReservedChars($string) {
    $regex = "/[\\+\\-\\=\\&\\|\\!\\(\\)\\{\\}\\[\\]\\^\\\"\\~\\*\\<\\>\\?\\:\\\\\\/]/";
    $string = preg_replace_callback ($regex, 
        function ($matches) { 
            return "\\" . $matches[0]; 
        }, $string); 
    return $string;
}
echo escapeElasticSearchReservedChars($string);

Output: The next chars should be escaped\: \+ \- \= \&\& \|\| \> \< \! \( \) \{ \} \[ \] \^ \" \~ \* \? \: \\ \/ Did it work\?

like image 135
Louis Barranqueiro Avatar answered Sep 21 '22 00:09

Louis Barranqueiro