3v4l.org

run code in 200+ php & hhvm versions
<?php $code = ' <pre class="prettyprint"><?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title> SimpleXML Example</title> </head> <body> <h1> Please go <a href="http://www.ucertify.com">http://www.ucertify.com</a> <br/> </h1> </body> </html> </pre> Which of the following statements will display the HREF attribute on the anchor tag if the SimpleXML object is $sxml? | In the script given in the question, HREF is the attribute of the anchor (<a>) tag. Besides this, anchor tag is inside the <h1> tag and <body> tag. Hence, you will access the HREF tag in the following way: <pre class="prettyprint">$sxml->body->h1->a[\'href\']</pre> Fact What is SimpleXML? SimpleXML is a PHP extension that allows users to easily manipulate/use XML data. Its functions let a user convert XML to an object, which can be processed with property selectors and array iterators. Consider the following XML file, which describes a small collection of books in XML format. It has a root node of store having a direct child, i.e., category, which classifies the books as fiction; category has two children() labelled book; "twilight" by Stephinie Mayer and "The Bikers" by Alex R. Stuart. <pre class="prettyprint"><?xml version="1.0"?> <store> <category id="fiction"> <book> <title>twilight</title> <author>Stephinie Mayer</author> </book> <book> <title>The Bikers</title> <author>Alex R. Stuart</author> </book> </category> </store> </pre> To extract the book title and author, a user will run the following PHP script: <pre class="prettyprint"><?php $store = simplexml_load_file(\'store.xml\'); foreach ($store->category as $category) { printf("Category %s\n", $category[\'id\']); foreach ($category->book as $book) { printf("<br>Title: %s\n", $book->title); printf("<br>Author: %s\n", $book->author); } } ?></pre> '; \libxml_use_internal_errors(true); $dom = new \DomDocument('1.0', 'utf8'); $dom->loadHtml($code, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $elements = $dom->getElementsByTagName('pre'); foreach($elements as $code){ $newNode = $dom->createElement('pre'); $newNode->setAttribute('class', 'prettyprint'); $text = new \DOMText(htmlentities($code->nodeValue)); $newNode->appendChild($text); $code->parentNode->replaceChild($newNode, $code); } var_dump($dom->saveHtml());
based on WLGjB
Output for 7.1.25 - 7.3.5
string(1796) "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <pre class="prettyprint"> SimpleXML Example Please go http://www.ucertify.com </pre><p> Which of the following statements will display the HREF attribute on the anchor tag if the SimpleXML object is $sxml? | In the script given in the question, HREF is the attribute of the anchor (<a>) tag. Besides this, anchor tag is inside the <h1> tag and <body> tag. Hence, you will access the HREF tag in the following way: <pre class="prettyprint">$sxml-&amp;gt;body-&amp;gt;h1-&amp;gt;a['href']</pre> Fact What is SimpleXML? SimpleXML is a PHP extension that allows users to easily manipulate/use XML data. Its functions let a user convert XML to an object, which can be processed with property selectors and array iterators. Consider the following XML file, which describes a small collection of books in XML format. It has a root node of store having a direct child, i.e., category, which classifies the books as fiction; category has two children() labelled book; "twilight" by Stephinie Mayer and "The Bikers" by Alex R. Stuart. <pre class="prettyprint"> twilight Stephinie Mayer The Bikers Alex R. Stuart </pre> To extract the book title and author, a user will run the following PHP script: <pre class="prettyprint">category as $category) { printf(&amp;quot;Category %s\n&amp;quot;, $category['id']); foreach ($category-&amp;gt;book as $book) { printf(&amp;quot;Title: %s\n&amp;quot;, $book-&amp;gt;title); printf(&amp;quot;Author: %s\n&amp;quot;, $book-&amp;gt;author); } } ?&amp;gt;</pre> </body></h1></a></p> "