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? 
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\?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With