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!
Output for 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Deprecated: mb_convert_encoding(): Handling HTML entities via mbstring is deprecated; use htmlspecialchars, htmlentities, or mb_encode_numericentity/mb_decode_numericentity instead in /in/Pluib on line 20 <blockquote><p>Life is like riding a bicycle. To keep your balance, you must keep moving.</p></blockquote> Fatal error: Uncaught Exception: no elements found in /in/Pluib:31 Stack trace: #0 /in/Pluib(36): ExtractTag->getFirstTag('img') #1 /in/Pluib(41): ExtractTag->printFirstTag('img') #2 /in/Pluib(51): ExtractTag->print_image() #3 {main} thrown in /in/Pluib on line 31
Process exited with code 255.
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27
<blockquote><p>Life is like riding a bicycle. To keep your balance, you must keep moving.</p></blockquote> Fatal error: Uncaught Exception: no elements found in /in/Pluib:31 Stack trace: #0 /in/Pluib(36): ExtractTag->getFirstTag('img') #1 /in/Pluib(41): ExtractTag->printFirstTag('img') #2 /in/Pluib(51): ExtractTag->print_image() #3 {main} thrown in /in/Pluib on line 31
Process exited with code 255.
Output for 7.1.25 - 7.1.30
Fatal error: Uncaught Error: Call to undefined method DOMNodeList::count() in /in/Pluib:30 Stack trace: #0 /in/Pluib(36): ExtractTag->getFirstTag('blockquote') #1 /in/Pluib(44): ExtractTag->printFirstTag('blockquote') #2 /in/Pluib(50): ExtractTag->print_quote() #3 {main} thrown in /in/Pluib on line 30
Process exited with code 255.

preferences:
171.09 ms | 401 KiB | 165 Q