-1

I am using Doctrine MongoDB and I have the following code in the query builder:

$regexSeller = new Regex('^'.$search['seller'], 'i');
$raw->addOr($builder->matchExpr()->field('seller')->equals($regexSeller));

It works and find the good results with a search like this "Bob" but doesn't work when there are parentheses like "Bob (from Chicago)".

I guess the problem is in the first line but can't find a way to match with parentheses too.

cyclone200
  • 367
  • 7
  • 22
  • You should probably apply some sort of masking/escaping method to the _data_ you are inserting into a regular expression here. Using PHP preg functionality, that would be the job of https://www.php.net/manual/en/function.preg-quote.php; check if whatever class is used here has its own dedicated method for that first maybe. – misorude Feb 17 '20 at 12:51

1 Answers1

2

You have to escape the special characters of your query. E.g. replace ( with \( and ) with \).

In PHP you can use preg_quote(https://www.php.net/manual/en/function.preg-quote.php), like this:

$regexSeller = new Regex('^'.preg_quote($search['seller']), 'i');

Let me know if it worked! ;)

Thanks!

Daniel Mihai
  • 147
  • 1
  • 6