3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $rules = [ 'weekly_rate' => 'required, /^\d*\.\d+%?$/' ]; $_POST['weekly_rate'] = '0.012%'; function validate ($rule_bag, $input) { $flag = true; $error_bag = []; foreach ($rule_bag as $item => $rules){ $rules = is_array($rules) ? $rules : array_filter(explode(',', $rules)); foreach($rules as $rule){ $rule = trim($rule); switch(mb_strtolower(trim($rule))){ case 'required': { if (!isset($input[$item])) { $flag = false; !isset($error_bag[$item])?$error_bag[$item]=[]:null; $error_bag[$item][] = $rule; } break; } default: { if (isset($input[$item])){ try { if (!preg_match($rule, $input[$item])){ $flag = false; !isset($error_bag[$item])?$error_bag[$item]=[]:null; !isset($error_bag[$item]['regex'])?$error_bag[$item]['regex']=[]:null; $error_bag[$item]['regex'][] = $rule; } } catch(Exception $e){ echo $e->getMessage(); } } } } } } return $flag ? $flag : $error_bag; } var_dump(validate($rules,$_POST));
based on BujRJ
Output for 5.6.0 - 7.3.0alpha1
bool(true)