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

matchAt() public static method

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
return integer | null Index of match, or null
    public static function matchAt($regex, $string, $offset = 0)
    {
        $matches = [];
        $string = mb_substr($string, $offset, null, 'utf-8');
        if (!preg_match($regex, $string, $matches, PREG_OFFSET_CAPTURE)) {
            return;
        }
        // PREG_OFFSET_CAPTURE always returns the byte offset, not the char offset, which is annoying
        $charPos = mb_strlen(mb_strcut($string, 0, $matches[0][1], 'utf-8'), 'utf-8');
        return $offset + $charPos;
    }

Usage Example

Ejemplo n.º 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;
 }
All Usage Examples Of League\CommonMark\Util\RegexHelper::matchAt