How to return only named groups with preg_match or preg_match_all?

前端 未结 9 1370
余生分开走
余生分开走 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:07

    I use some of introduced codes and this is the final code works on php 5.6+:

    $re = '/\d+\r\n(?<start>[\d\0:]+),\d+\s--\>\s(?<end>[\d\0:]+),.*\r\nHOME.*\r\nGPS\((?<x>[\d\.]+),(?<y>[\d\.]+),(?<d>[\d\.]+)\)\sBAROMETER\:(?<h>[\d\.]+)/';
    
    $str= file_get_contents($srtFile);
    preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
    echo '<pre>';
    $filtered=array_map(function ($d){
         return $array_filtered = array_filter($d, "is_string", ARRAY_FILTER_USE_KEY);
        },$matches);
    var_dump($filtered);
    

    if you are interested what it does it read position data from a str file that DJI drones generate while recording video.

    0 讨论(0)
  • 2020-12-09 11:09

    You could use T-Regx and go with group() or namedGroups() which only returns named capturing groups.

    <?php
    $subject = "This is some text written on 2010-07-18.";
    
    pattern('(?<date>\d\d\d\d-\d\d-\d\d)', 'i')->match($subject)->first(function ($match) {
    
        $date = $match->get('date'); 
        // 2010-07-18
    
        $groups = $match->namedGroups(); 
        // [
        //   'date' => '2010-07-18'
        // ]   
    });
    
    0 讨论(0)
  • 2020-12-09 11:10

    Similar to the answer that hakre posted above, I use this snippet to get just the named parameters:

    $subject = "This is some text written on 2010-07-18.";
    $pattern = '|(?<date>\d\d\d\d-\d\d-\d\d)|i';
    preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
    echo '<pre>Before Diff: ', print_r($matches, 1), '</pre>';
    $matches = array_diff_key($matches[0], range(0, count($matches[0])));
    echo '<pre>After Diff: ', print_r($matches, 1), '</pre>';
    

    ...which produces this:

    Before Array
    (
        [0] => Array
            (
                [0] => 2010-07-18
                [date] => 2010-07-18
                [1] => 2010-07-18
            )
    
    )
    After Array
    (
        [date] => 2010-07-18
    )
    
    0 讨论(0)
  • 2020-12-09 11:14

    I do not think you can make preg_* do it, but you can do it with a simple loop. But I don't see why those elements pose a problem.

    0 讨论(0)
  • 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')));
    
    0 讨论(0)
  • 2020-12-09 11:20

    It also possible to unset all numeric indexes before return:

    foreach (range(0, floor(count($arr_result) / 2)) as $index) {
        unset($arr_result[$index]);
    }
    
    0 讨论(0)
提交回复
热议问题