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!
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Pluib
function name:  (null)
number of ops:  12
compiled vars:  !0 = $html, !1 = $dom
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, '%3Carticle+id%3D%22p73%22+class%3D%22p-quote%22%3E%0AAlbert+Einstein%E2%80%99s+Quotes+Collection%0A%3Cblockquote%3E%3Cp%3ELife+is+like+riding+a+bicycle.+To+keep+your+balance%2C+you+must+keep+moving.%3C%2Fp%3E%3C%2Fblockquote%3E%0A%3Cblockquote%3E%3Cp%3EPure+mathematics+is%2C+in+its+way%2C+the+poetry+of+logical+ideas.%3C%2Fp%3E%3C%2Fblockquote%3E%0A%3Cblockquote%3E%3Cp%3EEducation+is+what+remains+after+one+has+forgotten+what+one+has+learned+in+school.%3C%2Fp%3E%3C%2Fblockquote%3E%0A%3Cblockquote%3E%3Cp%3ELearn+from+yesterday%2C+live+for+today%2C+hope+for+tomorrow.+The+important+thing+is+not+to+stop+questioning.%3C%2Fp%3E%3C%2Fblockquote%3E%0A%3Cblockquote%3E%3Cp%3ELogic+will+get+you+from+A+to+B.+Imagination+will+take+you+everywhere.%3C%2Fp%3E%3C%2Fblockquote%3E%0A%3Cblockquote%3E%3Cp%3EIf+you+can%E2%80%99t+explain+it+simply%2C+you+don%E2%80%99t+understand+it+well+enough.%3C%2Fp%3E%3C%2Fblockquote%3E%0A%3Cblockquote%3E%3Cp%3EThe+difference+between+stupidity+and+genius+is+that+genius+has+its+limits.%3C%2Fp%3E%3C%2Fblockquote%3E%0A%3C%2Farticle%3E'
   49     1        NEW                                              $3      'ExtractTag'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $3
   50     5        INIT_METHOD_CALL                                         !1, 'print_quote'
          6        DO_FCALL                                      0  $6      
          7        ECHO                                                     $6
   51     8        INIT_METHOD_CALL                                         !1, 'print_image'
          9        DO_FCALL                                      0  $7      
         10        ECHO                                                     $7
         11      > RETURN                                                   1

Class ExtractTag:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Pluib
function name:  __construct
number of ops:  18
compiled vars:  !0 = $html, !1 = $content
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
   19     1        NEW                                              $3      'DomDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN_OBJ                                               'doc'
          4        OP_DATA                                                  $3
   20     5        INIT_FCALL                                               'mb_convert_encoding'
   21     6        SEND_VAR                                                 !0
   22     7        SEND_VAL                                                 'HTML-ENTITIES'
   23     8        SEND_VAL                                                 'UTF-8'
   20     9        DO_ICALL                                         $5      
         10        ASSIGN                                                   !1, $5
   25    11        BEGIN_SILENCE                                    ~7      
         12        FETCH_OBJ_R                                      ~8      'doc'
         13        INIT_METHOD_CALL                                         ~8, 'loadHTML'
         14        SEND_VAR_EX                                              !1
         15        DO_FCALL                                      0          
         16        END_SILENCE                                              ~7
   26    17      > RETURN                                                   null

End of function __construct

Function getfirsttag:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Pluib
function name:  getFirstTag
number of ops:  19
compiled vars:  !0 = $tag, !1 = $elements
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
   29     1        FETCH_OBJ_R                                      ~2      'doc'
          2        INIT_METHOD_CALL                                         ~2, 'getElementsByTagName'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $3      
          5        ASSIGN                                                   !1, $3
   30     6        INIT_METHOD_CALL                                         !1, 'count'
          7        DO_FCALL                                      0  $5      
          8        BOOL_NOT                                         ~6      $5
          9      > JMPZ                                                     ~6, ->14
   31    10    >   NEW                                              $7      'Exception'
         11        SEND_VAL_EX                                              'no+elements+found'
         12        DO_FCALL                                      0          
         13      > THROW                                         0          $7
   33    14    >   INIT_METHOD_CALL                                         !1, 'item'
         15        SEND_VAL_EX                                              0
         16        DO_FCALL                                      0  $9      
         17      > RETURN                                                   $9
   34    18*     > RETURN                                                   null

End of function getfirsttag

Function printfirsttag:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Pluib
function name:  printFirstTag
number of ops:  11
compiled vars:  !0 = $tag, !1 = $firstTag
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
   36     1        INIT_METHOD_CALL                                         'getFirstTag'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $2      
          4        ASSIGN                                                   !1, $2
   37     5        FETCH_OBJ_R                                      ~4      'doc'
          6        INIT_METHOD_CALL                                         ~4, 'saveHTML'
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0  $5      
          9        ECHO                                                     $5
   38    10      > RETURN                                                   null

End of function printfirsttag

Function print_image:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Pluib
function name:  print_image
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   INIT_METHOD_CALL                                         'printFirstTag'
          1        SEND_VAL                                                 'img'
          2        DO_FCALL                                      0          
   42     3      > RETURN                                                   null

End of function print_image

Function print_quote:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Pluib
function name:  print_quote
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   INIT_METHOD_CALL                                         'printFirstTag'
          1        SEND_VAL                                                 'blockquote'
          2        DO_FCALL                                      0          
   45     3      > RETURN                                                   null

End of function print_quote

End of class ExtractTag.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
231.47 ms | 1009 KiB | 14 Q