/** * @param Text $text * * @return array */ protected function parseAttributes(Text $text) { $patterns = ['id' => '/^#([a-zA-Z0-9_-]+)/', 'class' => '/^\\.([a-zA-Z0-9_-]+)/', 'attr' => '/^\\[([^\\]]+)\\]/', 'ident' => '/^(.)/']; $tokens = ['id' => [], 'class' => [], 'attr' => [], 'ident' => []]; while (!$text->isEmpty()) { foreach ($patterns as $name => $pattern) { if ($text->match($pattern, $matches)) { $tokens[$name][] = $matches[1]; $text->setString(substr($text->getString(), strlen($matches[0]))); break; } } } $attributes = array(); if (count($tokens['id'])) { $attributes['id'] = array_pop($tokens['id']); } if (count($tokens['class'])) { $attributes['class'] = implode(' ', $tokens['class']); } if (count($tokens['attr'])) { foreach ($tokens['attr'] as $raw) { $items = explode(' ', $raw); foreach ($items as $item) { if (strpos($item, '=') !== false) { list($key, $value) = explode('=', $item); $attributes[$key] = trim($value, '"'); } else { $attributes[$item] = $item; } } } } return $attributes; }
public function testMatch() { $text = new Text('abcd1234efgh'); $this->assertTrue($text->match('/\\d{4}/')); $this->assertFalse($text->match('/^\\d+/')); }