Andrew Goldenberg Andrew Goldenberg - 5 months ago 10
PHP Question

Determining A list of IDs filtered out of a string

So I'm working with an API that allows developers to apply filters.

Currently there's an endpoint of /users?filters=user_id:10,20,30,40 etc. but it also accepts more in line filters. For excample

/users?params=user_id:10,20,30,name:John,Date:20160601

What I'd like to know is the best way to go about determining a couple of things.

If user_id filter is set. Only get the IDs from the filter.

Answer
  <?php
     //Make sure the filter is set
     if(!empty($_GET['filters'])){
         $filters = $_GET['filters'];
         if(strpos($filters,'user_id') >= 0){
            preg_match('/user_id:[0-9 ,]+/',$filters,$matches);
            if(count($matches[0])){
                //Parse IDs
                $ids = substr($matches[0],(strpos($matches[0],":")+1),(strlen($matches[0])-strpos($matches[0],":")));
                $ids = explode(',',$ids);
                //In case we take in an extra comma
                if($ids[count($ids)-1] == ""){
                   unset($ids[count($ids)-1]);
                }
                //Do something with IDs here
                var_dump($ids);
             }
         }else{
            echo 'not set';
         }
     }else{
           echo 'No filters';
     }