Jyxo\HtmlTag::createFromSource PHP Method

createFromSource() public static method

The first and last tag will be used as the opening and closing tag respectively. Anything between those tags will be used as contents.
public static createFromSource ( string $html ) : self
$html string HTML source code
return self
    public static function createFromSource(string $html) : self
    {
        if (preg_match('~<(\\w+)(\\s[^>]*)+>(.*)((<[^>]+>)?[^>]*)$~', $html, $matches)) {
            $tag = new self($matches[1]);
            if ('' !== $matches[3]) {
                // @todo Maybe some kind of recursion to create a tree of elements
                $tag->setText($matches[3]);
            }
            if (preg_match_all('/(\\w+)\\s*=\\s*"([^"]+)"/', $matches[2], $submatches, PREG_PATTERN_ORDER)) {
                $attrs = array_combine($submatches[1], $submatches[2]);
                $tag->setAttributes($attrs);
            }
            return $tag;
        }
        throw new \InvalidArgumentException('Zadaný text neobsahuje validní html');
    }