PHPHtmlParser\Dom\AbstractNode::ancestorByTag PHP Method

ancestorByTag() public method

Function to locate a specific ancestor tag in the path to the root.
public ancestorByTag ( string $tag ) : AbstractNode
$tag string
return AbstractNode
    public function ancestorByTag($tag)
    {
        // Start by including ourselves in the comparison.
        $node = $this;
        while (!is_null($node)) {
            if ($node->tag->name() == $tag) {
                return $node;
            }
            $node = $node->getParent();
        }
        throw new ParentNotFoundException('Could not find an ancestor with "' . $tag . '" tag');
    }