繁体   English   中英

确定从字符串中过滤出的ID列表

[英]Determining A list of IDs filtered out of a string

因此,我正在使用允许开发人员应用过滤器的API。

当前有一个/ users?filters = user_id:10、20、30、40等的终结点,但它也接受更多的行过滤器。 举例

/ users?params = user_id:10,20,30,名称:John,日期:20160601

我想知道的是确定几件事情的最好方法。

如果设置了user_id过滤器。 仅从过滤器获取ID。

  <?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';
     }

您可以尝试结合使用isset()explode()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM