Skip to content

Commit 4cf1819

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

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/Search.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public function compiled() {
211211
public function escape($string)
212212
{
213213
$search = array("\\", "\x00", "\n", "\r", "'", '"', "\x1a");
214-
$replace = array("\\\\","\\0","\\n", "\\r", "\\'", '\"', "\\Z");
214+
$replace = array("\\","\\0","\\n", "\\r", "\\'", '\"', "\\Z");
215215
return str_replace($search, $replace, $string);
216216
}
217217

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

302302
if (is_string($value_or_array_or_callable) || is_numeric($value_or_array_or_callable)) {
303303
$value = trim($value_or_array_or_callable);
304+
$value = $this->escapeReservedCharacters($value);
304305

305306
// Check if there's already a where clause created, unless we're at the start of a group
306307
if ($this->components['find'] && substr($this->components['find'], -1) !== '(') {
@@ -358,4 +359,17 @@ public function notFind($value_or_array_or_callable)
358359
{
359360
return $this->find($value_or_array_or_callable, 'andnot');
360361
}
362+
363+
/*
364+
* Escape with a backslash reserved characters
365+
* https://resources.docs.salesforce.com/sfdc/pdf/salesforce_soql_sosl.pdf
366+
*/
367+
private function escapeReservedCharacters($q)
368+
{
369+
$search = ["?", "&", "|", "!", "{", "}", "[", "]", "(", ")", "^", "~", "*", ":", "\\", '"', "'", "+", "-"];
370+
$replace = ["\?", "\&", "\|", "\!", "\{", "\}", "\[", "\]", "\(", "\)", "\^", "\~", "\*", "\:", "\\", '\"', "\'", "\+", "\-"];
371+
$q = str_replace($search, $replace, $q);
372+
373+
return $q;
374+
}
361375
}

0 commit comments

Comments
 (0)