DiDom\Document::appendChild PHP Method

appendChild() public method

Add new child at the end of the children.
public appendChild ( Element | DOMNode | array $nodes ) : Document
$nodes Element | DOMNode | array The appended child
return Document
    public function appendChild($nodes)
    {
        $nodes = is_array($nodes) ? $nodes : [$nodes];
        foreach ($nodes as $node) {
            if ($node instanceof Element) {
                $node = $node->getNode();
            }
            if (!$node instanceof \DOMNode) {
                throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of %s\\Element or DOMNode, %s given', __METHOD__, __NAMESPACE__, is_object($node) ? get_class($node) : gettype($node)));
            }
            Errors::disable();
            $cloned = $node->cloneNode(true);
            $newNode = $this->document->importNode($cloned, true);
            $this->document->appendChild($newNode);
            Errors::restore();
        }
        return $this;
    }

Usage Example

Example #1
0
 public function findNextNodeByText(DiDomElement $element, $expression, $grep = false)
 {
     $childNodes = $element->getNode()->childNodes;
     $length = $childNodes->length;
     $stop = false;
     for ($i = 0; $i < $length; $i++) {
         $node = $childNodes->item($i);
         if ($stop && trim($node->textContent)) {
             $document = new DiDomDocument();
             $document->appendChild($node);
             return $document;
         } else {
             if (!$grep && $expression == $node->textContent) {
                 $stop = true;
             } else {
                 if ($grep && preg_match($expression, $node->textContent)) {
                     $stop = true;
                 }
             }
         }
     }
 }
All Usage Examples Of DiDom\Document::appendChild