1

I am trying to get both fields in an api enpoint so that it queries based on that. example.

api/evenSession/123123123/eventSession?token=1234656&filter= chris moreno

my controller searches by first or last name. But i want to create an endpoint that it queries the database if it's passed in "chris moreno"

This is my $filter code

if(preg_match_all('/^[A-Z]*[a-z]*(-|\s)[A-Z]*[a-z]*$/', $filter)){
       $searchFields = [
           'a.lastName',
           'a.firstName'
       ];

I'd like to be able to catch both fields (chris and moreno) in my filter so that I can pass that over to my query so that it can check for people checked in with first and last name (narrow the search)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Could you clarify what you want? Are you asking for the search parameters, or how to derive them from the fields in the URL? – shn Jun 13 '19 at 18:29
  • I am trying to get both filter fields. example Chris & moreno. Right now, I get only Chris OR Chris moreno as ONE string. id like to be able to get both Chris and Moreno as seperate strings so i can put them in seperate variables so that i can query –  Jun 13 '19 at 18:29
  • @shn yes. I am trying to filter the my api endpoint so that it can query by both first and last name. Right now, that API endpoint only queries if you put chris (first name) or moreno (last name). I want to be able to filter a query by both first and last name –  Jun 13 '19 at 18:49

1 Answers1

1

Here, we would have three capturing groups, if our inputs are as listed, for instance, an expression we would start with:

token=(.*?)&filter=\s*([\w]+)\s+([\w]+)

Demo 1

Here, we our token value in this capturing group:

(.*?)

and our names in these two separate capturing groups:

([\w]+)

which the last two groups can be modified, if we have names such as O'Neal, that may not fall into [A-Za-z] char class. For instance, we can allow any chars:

token=(.*?)&filter=\s*(.+?)\s+(.+) 

Demo 2

Test

$re = '/token=(.*?)&filter=\s*([\w]+)\s+([\w]+)/m';
$str = 'api/evenSession/123123123/eventSession?token=1234656&filter= chris moreno
api/evenSession/123123123/eventSession?token=1234656&filter= alice bob
';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

$searchFields = [];
foreach ($matches as $key => $value) {
    $searchFields[$key] = [$value[2], $value[3]];
}

var_dump($searchFields);

Demo 3

Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    what id like to do is to be able to capture both filter fields separately. for example. I want to capture Chris and Moreno separately. I then want to be able to pass these into Doctrine so i can query for people that are checking in by first and last name. –  Jun 13 '19 at 18:32
  • 1
    is there a way to capture just chris in one group and moreno in another group? –  Jun 13 '19 at 19:52