<?php $pattern = "/^(?<animal>DOG|CAT)?(?<color>BLUE|RED)?$/i"; $str = "DOG"; if (preg_match($pattern, $str, $matches)) { $matches = array_merge(array('animal' => '', 'color' => ''), $matches); $matches = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY); print_r($matches); }
You have javascript disabled. You will not be able to edit any code.