Skip to content

Commit d1187ae

Browse files
committed
Escape with a backslash reserved characters
ref: https://salesforce.stackexchange.com/a/38661
1 parent fa07803 commit d1187ae

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/Search.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public function compiled() {
200200
public function escape($string)
201201
{
202202
$search = array("\\", "\x00", "\n", "\r", "'", '"', "\x1a");
203-
$replace = array("\\\\","\\0","\\n", "\\r", "\\'", '\"', "\\Z");
203+
$replace = array("\\","\\0","\\n", "\\r", "\\'", '\"', "\\Z");
204204
return str_replace($search, $replace, $string);
205205
}
206206

@@ -285,6 +285,7 @@ public function find($value_or_array_or_callable, $join = 'and') {
285285

286286
if (is_string($value_or_array_or_callable) || is_numeric($value_or_array_or_callable)) {
287287
$value = trim($value_or_array_or_callable);
288+
$value = $this->escapeReservedCharacters($value);
288289

289290
// Check if there's already a where clause created, unless we're at the start of a group
290291
if ($this->components['find'] && substr($this->components['find'], -1) !== '(') {
@@ -342,4 +343,17 @@ public function notFind($value_or_array_or_callable)
342343
{
343344
return $this->find($value_or_array_or_callable, 'andnot');
344345
}
345-
}
346+
347+
/*
348+
* Escape with a backslash reserved characters
349+
* https://resources.docs.salesforce.com/sfdc/pdf/salesforce_soql_sosl.pdf
350+
*/
351+
private function escapeReservedCharacters($q)
352+
{
353+
$search = ["?", "&", "|", "!", "{", "}", "[", "]", "(", ")", "^", "~", "*", ":", "\\", '"', "'", "+", "-"];
354+
$replace = ["\?", "\&", "\|", "\!", "\{", "\}", "\[", "\]", "\(", "\)", "\^", "\~", "\*", "\:", "\\", '\"', "\'", "\+", "\-"];
355+
$q = str_replace($search, $replace, $q);
356+
357+
return $q;
358+
}
359+
}

0 commit comments

Comments
 (0)