League\CommonMark\Cursor::match PHP Method

match() public method

Returns the matching text and advances to the end of that match
public match ( string $regex ) : string | null
$regex string
return string | null
    public function match($regex)
    {
        $subject = $this->getRemainder();
        $matches = [];
        if (!preg_match($regex, $subject, $matches, PREG_OFFSET_CAPTURE)) {
            return;
        }
        // PREG_OFFSET_CAPTURE always returns the byte offset, not the char offset, which is annoying
        $offset = mb_strlen(mb_strcut($subject, 0, $matches[0][1], 'utf-8'), 'utf-8');
        // [0][0] contains the matched text
        // [0][1] contains the index of that match
        $this->advanceBy($offset + mb_strlen($matches[0][0], 'utf-8'));
        return $matches[0][0];
    }

Usage Example

Beispiel #1
0
 /**
  * @param  string $mediaType
  * @return mixed
  */
 protected function getElementParts($mediaType)
 {
     $label = trim($this->cursor->match('/^\\[(?:[^\\\\\\[\\]]|\\\\[\\[\\]]){0,750}\\]/'), '[]');
     $url = trim(LinkParserHelper::parseLinkDestination($this->cursor), '()');
     if ($label === null || $url === null) {
         return $this->cancel();
     }
     return $this->getElement($mediaType, $label, $url);
 }
All Usage Examples Of League\CommonMark\Cursor::match