Andrew Goldenberg Andrew Goldenberg - 2 years ago 100
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


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 Source
     //Make sure the filter is set
         $filters = $_GET['filters'];
         if(strpos($filters,'user_id') >= 0){
            preg_match('/user_id:[0-9 ,]+/',$filters,$matches);
                //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] == ""){
                //Do something with IDs here
            echo 'not set';
           echo 'No filters';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download