League\CommonMark\Util\RegexHelper::matchAll PHP Method

matchAll() public static method

Functional wrapper around preg_match_all
public static matchAll ( string $pattern, string $subject, integer $offset ) : array | null
$pattern string
$subject string
$offset integer
return array | null
    public static function matchAll($pattern, $subject, $offset = 0)
    {
        $matches = [];
        $subject = substr($subject, $offset);
        preg_match_all($pattern, $subject, $matches, PREG_PATTERN_ORDER);
        $fullMatches = reset($matches);
        if (empty($fullMatches)) {
            return;
        }
        if (count($fullMatches) === 1) {
            foreach ($matches as &$match) {
                $match = reset($match);
            }
        }
        if (!empty($matches)) {
            return $matches;
        }
    }

Usage Example

Beispiel #1
0
 /**
  * @param ContextInterface $context
  * @param Cursor $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     $tmpCursor = clone $cursor;
     $indent = $tmpCursor->advanceWhileMatches(' ', 3);
     $rest = $tmpCursor->getRemainder();
     $data = new ListData();
     if ($matches = RegexHelper::matchAll('/^[*+-]( +|$)/', $rest)) {
         $spacesAfterMarker = strlen($matches[1]);
         $data->type = ListBlock::TYPE_UNORDERED;
         $data->delimiter = null;
         $data->bulletChar = $matches[0][0];
     } elseif ($matches = RegexHelper::matchAll('/^(\\d+)([.)])( +|$)/', $rest)) {
         $spacesAfterMarker = strlen($matches[3]);
         $data->type = ListBlock::TYPE_ORDERED;
         $data->start = intval($matches[1]);
         $data->delimiter = $matches[2];
         $data->bulletChar = null;
     } else {
         return false;
     }
     $data->padding = $this->calculateListMarkerPadding($matches[0], $spacesAfterMarker, $rest);
     $cursor->advanceToFirstNonSpace();
     $cursor->advanceBy($data->padding);
     // list item
     $data->markerOffset = $indent;
     // add the list if needed
     $container = $context->getContainer();
     if (!$container || !$context->getContainer() instanceof ListBlock || !$data->equals($container->getListData())) {
         $context->addBlock(new ListBlock($data));
     }
     // add the list item
     $context->addBlock(new ListItem($data));
     return true;
 }
All Usage Examples Of League\CommonMark\Util\RegexHelper::matchAll