matchAt() 공개 정적인 메소드

Attempt to match a regex in string s at offset offset
public static matchAt ( string $regex, string $string, integer $offset ) : integer | null
$regex string
$string string
$offset integer
리턴 integer | null Index of match, or null
예제 #1
0
 /**
  * Returns the position of the next non-space character
  *
  * @return int
  */
 public function getFirstNonSpacePosition()
 {
     if ($this->firstNonSpaceCache === null) {
         $match = RegexHelper::matchAt('/[^ ]/', $this->line, $this->currentPosition);
         $this->firstNonSpaceCache = $match === null ? $this->length : $match;
     }
     return $this->firstNonSpaceCache;
 }
예제 #2
0
 /**
  * @param ContextInterface $context
  * @param Cursor $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     $match = RegexHelper::matchAt(RegexHelper::getInstance()->getHtmlBlockOpenRegex(), $cursor->getLine(), $cursor->getFirstNonSpacePosition());
     if ($match === null) {
         return false;
     }
     $context->addBlock(new HtmlBlock());
     $context->setBlocksParsed(true);
     return true;
 }
 /**
  * @param ContextInterface $context
  * @param Cursor           $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     if ($cursor->isIndented()) {
         return false;
     }
     $match = RegexHelper::matchAt(RegexHelper::getInstance()->getThematicBreakRegex(), $cursor->getLine(), $cursor->getFirstNonSpacePosition());
     if ($match === null) {
         return false;
     }
     // Advance to the end of the string, consuming the entire line (of the thematic break)
     $cursor->advanceBy(mb_strlen($cursor->getRemainder()));
     $context->addBlock(new ThematicBreak());
     $context->setBlocksParsed(true);
     return true;
 }
예제 #4
0
 /**
  * @param ContextInterface $context
  * @param Cursor           $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     if ($cursor->isIndented() && !$context->getContainer() instanceof ListBlock) {
         return false;
     }
     $tmpCursor = clone $cursor;
     $tmpCursor->advanceToFirstNonSpace();
     $rest = $tmpCursor->getRemainder();
     $data = new ListData();
     $data->markerOffset = $cursor->getIndent();
     if ($matches = RegexHelper::matchAll('/^[*+-]/', $rest)) {
         $data->type = ListBlock::TYPE_UNORDERED;
         $data->delimiter = null;
         $data->bulletChar = $matches[0][0];
     } elseif (($matches = RegexHelper::matchAll('/^(\\d{1,9})([.)])/', $rest)) && (!$context->getContainer() instanceof Paragraph || $matches[1] === '1')) {
         $data->type = ListBlock::TYPE_ORDERED;
         $data->start = intval($matches[1]);
         $data->delimiter = $matches[2];
         $data->bulletChar = null;
     } else {
         return false;
     }
     $markerLength = strlen($matches[0]);
     // Make sure we have spaces after
     $nextChar = $tmpCursor->peek($markerLength);
     if (!($nextChar === null || $nextChar === "\t" || $nextChar === ' ')) {
         return false;
     }
     // If it interrupts paragraph, make sure first line isn't blank
     if ($context->getContainer() instanceof Paragraph && !RegexHelper::matchAt(RegexHelper::REGEX_NON_SPACE, $rest, $markerLength)) {
         return false;
     }
     // We've got a match! Advance offset and calculate padding
     $cursor->advanceToFirstNonSpace();
     // to start of marker
     $cursor->advanceBy($markerLength, true);
     // to end of marker
     $data->padding = $this->calculateListMarkerPadding($cursor, $markerLength);
     // 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;
 }
 /**
  * @param ContextInterface $context
  * @param Cursor           $cursor
  *
  * @return bool
  */
 public function parse(ContextInterface $context, Cursor $cursor)
 {
     if ($cursor->isIndented()) {
         return false;
     }
     if ($cursor->getFirstNonSpaceCharacter() !== '<') {
         return false;
     }
     $savedState = $cursor->saveState();
     $cursor->advanceToFirstNonSpace();
     $line = $cursor->getRemainder();
     for ($blockType = 1; $blockType <= 7; $blockType++) {
         $match = RegexHelper::matchAt(RegexHelper::getHtmlBlockOpenRegex($blockType), $line);
         if ($match !== null && ($blockType < 7 || !$context->getContainer() instanceof Paragraph)) {
             $cursor->restoreState($savedState);
             $context->addBlock(new HtmlBlock($blockType));
             $context->setBlocksParsed(true);
             return true;
         }
     }
     $cursor->restoreState($savedState);
     return false;
 }