Extracting matches from php regex

前端 未结 5 2087
深忆病人
深忆病人 2020-12-03 14:33

In perl regex we can extract the matched variables, ex below.

   # extract hours, minutes, seconds
   $time =~ /(\\d\\d):(\\d\\d):(\\d\\d)/; # match hh:mm         


        
5条回答
  •  难免孤独
    2020-12-03 15:04

    Check the php manual

    int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] )

    If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches1 will have the text that matched the first captured parenthesized subpattern, and so on.

    $subject = "E:contact@customer.com I:100955";
    $pattern = "/^E:(?\w+) I:(?\d+)$/";
    if (preg_match($pattern, $subject,$matches)) {
        print_r($matches);
    }
    

提交回复
热议问题