League\CommonMark\Cursor::getCharacter PHP Method

getCharacter() public method

public getCharacter ( integer | null $index = null ) : string | null
$index integer | null
return string | null
    public function getCharacter($index = null)
    {
        if ($index === null) {
            $index = $this->currentPosition;
        }
        // Index out-of-bounds, or we're at the end
        if ($index < 0 || $index >= $this->length) {
            return;
        }
        return mb_substr($this->line, $index, 1, 'utf-8');
    }

Usage Example

 public static function parse(Cursor $cursor)
 {
     if (null === self::$regexp) {
         $regex = RegexHelper::getInstance();
         self::$regexp = sprintf('/^\\s*([.#][_a-z0-9-]+|%s%s)(?<!})\\s*/i', $regex->getPartialRegex(RegexHelper::ATTRIBUTENAME), $regex->getPartialRegex(RegexHelper::ATTRIBUTEVALUESPEC));
     }
     $state = $cursor->saveState();
     $cursor->advanceToFirstNonSpace();
     if ('{' !== $cursor->getCharacter()) {
         $cursor->restoreState($state);
         return [];
     }
     $cursor->advanceBy(1);
     if (':' === $cursor->getCharacter()) {
         $cursor->advanceBy(1);
     }
     $attributes = [];
     while ($attribute = trim($cursor->match(self::$regexp))) {
         if ('#' === $attribute[0]) {
             $attributes['id'] = substr($attribute, 1);
             continue;
         }
         if ('.' === $attribute[0]) {
             $attributes['class'][] = substr($attribute, 1);
             continue;
         }
         list($name, $value) = explode('=', $attribute, 2);
         $first = $value[0];
         $last = substr($value, -1);
         if (('"' === $first && '"' === $last || "'" === $first && "'" === $last) && strlen($value) > 1) {
             $value = substr($value, 1, -1);
         }
         if ('class' === strtolower(trim($name))) {
             foreach (array_filter(explode(' ', trim($value))) as $class) {
                 $attributes['class'][] = $class;
             }
         } else {
             $attributes[trim($name)] = trim($value);
         }
     }
     if (0 === $cursor->advanceWhileMatches('}')) {
         $cursor->restoreState($state);
         return [];
     }
     if (isset($attributes['class'])) {
         $attributes['class'] = implode(' ', $attributes['class']);
     }
     return $attributes;
 }
All Usage Examples Of League\CommonMark\Cursor::getCharacter