tags in an HTML document
$html = 'Hello world!
How are you?
'; $dom = new DOMDocument(); $dom->loadHTML($html); $paragraphs = $dom->getElementsByTagName('p'); foreach ($paragraphs as $p) { echo $p->nodeValue . '
'; }
tags in the document and loop through them to output their inner text. The package library used in this example is the PHP DOM extension.
Example 2: Using getElementsByTagName to retrieve all
$xml = 'In this example, we start by creating an XML string and loading it into a DOMDocument object. We then use getElementsByTagName to retrieve all'; $dom = new DOMDocument(); $dom->loadXML($xml); $books = $dom->getElementsByTagName('book'); foreach ($books as $book) { $title = $book->getElementsByTagName('title')->item(0)->nodeValue; $author = $book->getElementsByTagName('author')->item(0)->nodeValue; echo $title . ' by ' . $author . ' Harry Potter J.K. Rowling The Lord of the Rings J.R.R. Tolkien
'; }