How to return only named groups with preg_match or preg_match_all?

前端 未结 9 1420
余生分开走
余生分开走 2020-12-09 10:51

Example:

$string = \"This is some text written on 2010-07-18.\";
preg_match(\'|(?\\d\\d\\d\\d-\\d\\d-\\d\\d)|i\', $string, $arr_result);
print_r(         


        
9条回答
  •  我在风中等你
    2020-12-09 11:17

    preg_match does not have any flag or option that it only returns named matches (yet). So what you want is not directly possible. However you can remove all items with non-fitting keys from your matches array and then you get what you're looking for:

    $matches = array_intersect_key($matches, array_flip(array('name', 'likes')));
    

提交回复
热议问题