3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class FilterQuery { protected $filters = []; public function __construct(string $filter) { $this->parseFilters($filter); } private function parseFilters(string $filter) { $filters = explode(';', $filter); foreach ($filters as $splitFilter) { $this->parseFilter($splitFilter); } } private function parseFilter(string $filter) { $splitFilter = explode(':', $filter); if (count($splitFilter) < 2) { return; } $this->filters[$splitFilter[0]] = $this->parseFilterValues($splitFilter[1]); } private function parseFilterValues(string $values) : array { return explode(',', $values); } public function getFilter($filterName) : array { if (!array_key_exists($filterName, $this->filters)) { return []; } return $this->filters[$filterName]; } } $filter = new FilterQuery('loginType:1,2;ipAddress:192.168.254.249'); print_r($filter->getFilter('loginType')); print_r($filter->getFilter('ipAddress')); print_r($filter->getFilter('username'));
based on MIgcM
Output for 7.0.0 - 7.3.0beta1
Array ( [0] => 1 [1] => 2 ) Array ( [0] => 192.168.254.249 ) Array ( )
Output for 5.4.13 - 5.6.28
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/NSjll on line 29
Process exited with code 255.
Output for 5.3.23 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/NSjll on line 4
Process exited with code 255.