3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<< HTML <article id="p73" class="p-quote"> Albert Einstein’s Quotes Collection <blockquote><p>Life is like riding a bicycle. To keep your balance, you must keep moving.</p></blockquote> <blockquote><p>Pure mathematics is, in its way, the poetry of logical ideas.</p></blockquote> <blockquote><p>Education is what remains after one has forgotten what one has learned in school.</p></blockquote> <blockquote><p>Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning.</p></blockquote> <blockquote><p>Logic will get you from A to B. Imagination will take you everywhere.</p></blockquote> <blockquote><p>If you can’t explain it simply, you don’t understand it well enough.</p></blockquote> <blockquote><p>The difference between stupidity and genius is that genius has its limits.</p></blockquote> </article> HTML; class ExtractTag { private $doc = null; public function __construct($html) { $this->doc = new DomDocument(); $content = mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ); @$this->doc->loadHTML($content); } private function getFirstTag($tag) { $elements = $this->doc->getElementsByTagName($tag); if (!$elements->count()) { throw new Exception('no elements found'); } return $elements->item(0); } private function printFirstTag($tag) { $firstTag = $this->getFirstTag($tag); echo $this->doc->saveHTML($firstTag); } public function print_image() { $this->printFirstTag('img'); } public function print_quote() { $this->printFirstTag('blockquote'); } } $dom = new ExtractTag($html); echo $dom->print_quote(); #Expect to see a <blockquote> echo $dom->print_image(); #Expect to see nothing since there is no <img> tag in the html!

preferences:
100.4 ms | 404 KiB | 5 Q