src/Matchers/Match.php
public static function findAll($string, $regex)
    {
        $count = preg_match_all($regex, $string, $matches, PREG_SET_ORDER);
        if (!$count) {
            return [];
        }

        $pos = 0;
        $groups = [];
        foreach ($matches as $group) {
            $captureBegin = 0;
            $match = array_shift($group);
            $matchBegin = strpos($string, $match, $pos);
            $captures = [
                [
                    'begin' => $matchBegin,
                    'end' => $matchBegin + strlen($match) - 1,
                    'token' => $match,
                ],
            ];
            foreach ($group as $capture) {
                $captureBegin = strpos($match, $capture, $captureBegin);
                $captures[] = [
                    'begin' => $matchBegin + $captureBegin,
                    'end' => $matchBegin + $captureBegin + strlen($capture) - 1,
                    'token' => $capture,
                ];
            }
            $groups[] = $captures;
            $pos += strlen($match) - 1;
        }

        return $groups;
    }
public static function findAll($string, $regex)
    {
        $count = preg_match_all($regex, $string, $matches, PREG_SET_ORDER);
        if (!$count) {
            return [];
        }

        $pos = 0;
        $groups = [];
        foreach ($matches as $group) {
            $captureBegin = 0;
            $match = array_shift($group);
            $matchBegin = strpos($string, $match, $pos);
            $captures = [
                [
                    'begin' => $matchBegin,
                    'end' => $matchBegin + strlen($match) - 1,
                    'token' => $match,
                ],
            ];
            foreach ($group as $capture) {
                $captureBegin = strpos($match, $capture, $captureBegin);
                $captures[] = [
                    'begin' => $matchBegin + $captureBegin,
                    'end' => $matchBegin + $captureBegin + strlen($capture) - 1,
                    'token' => $capture,
                ];
            }
            $groups[] = $captures;
            $pos += strlen($match) - 1;
        }

        return $groups;
    }
public static function findAll ( $ string , $ regex ) { $ count = preg_match_all ( $ regex , $ string , $ matches , PREG_SET_ORDER ) ; if ( ! $ count ) { return [ ] ; } $ pos = 0 ; $ groups = [ ] ; foreach ( $ matches as $ group ) { $ captureBegin = 0 ; $ match = array_shift ( $ group ) ; $ matchBegin = strpos ( $ string , $ match , $ pos ) ; $ captures = [ [ 'begin' => $ matchBegin , 'end' => $ matchBegin + strlen ( $ match ) - 1 , 'token' => $ match , ] , ] ; foreach ( $ group as $ capture ) { $ captureBegin = strpos ( $ match , $ capture , $ captureBegin ) ; $ captures [ ] = [ 'begin' => $ matchBegin + $ captureBegin , 'end' => $ matchBegin + $ captureBegin + strlen ( $ capture ) - 1 , 'token' => $ capture , ] ; } $ groups [ ] = $ captures ; $ pos += strlen ( $ match ) - 1 ; } return $ groups ; }
Find all occurences of regular expression in a string.

@param string $string String to search
@param string $regex  Regular expression with captures

@return array
Array of capture groups. Captures in a group have named indexes: 'begin', 'end', 'token'.
e.g. fishfish /(fish)/
array(
array(
array('begin' => 0, 'end' => 3, 'token' => 'fish'),
array('begin' => 0, 'end' => 3, 'token' => 'fish')
),
array(
array('begin' => 4, 'end' => 7, 'token' => 'fish'),
array('begin' => 4, 'end' => 7, 'token' => 'fish')
)
)
Find all occurences of regular expression in a string .