3v4l.org

run code in 500+ PHP versions simultaneously
<?php //declare(strict_types=1); ini_set('display_errors','On'); header("Content-Type: text/plain;charset=utf-8");ini_set('html_errors','0'); //header("Content-Type: text/html;charset=utf-8"); $html=<<<'HTML' <div class="singlepost"> <ul class="linha_status" style=""> <li>Status: <b>Objeto em trânsito - por favor aguarde</b></li> <li>Data : 24/10/2021 | Hora: 12:04</li> <li>Origem: Unidade de Tratamento - Jaboatao Dos Guararapes / PE</li> <li>Destino: Agência dos Correios - Cuitegi / PB</li> </ul> <ul class="linha_status" style=""> <li>Status: <b>Objeto em trânsito - por favor aguarde</b></li> <li>Data : 19/10/2021 | Hora: 00:03</li> <li>Origem: Unidade de Logística Integrada - Curitiba / PR</li> <li>Destino: Unidade de Tratamento - Recife / PE</li> </ul> <ul class="linha_status" style=""> <li>Status: <b>Fiscalização aduaneira finalizada</b></li> <li>Data : 18/10/2021 | Hora: 23:35</li> <li>Local: Unidade Operacional - Curitiba / PR</li> </ul> <ul class="linha_status" style=""> <li>Status: <b>Objeto recebido pelos Correios do Brasil</b></li> <li>Data : 16/10/2021 | Hora: 11:45</li> <li>Local: Unidade de Logística Integrada - Curitiba / PR</li> </ul> <ul class="linha_status" style=""> <li>Status: <b>Objeto postado</b></li> <li>Data : 14/10/2021 | Hora: 20:30</li> <li>Local: País - / </li> </ul> </div> HTML; function json_encode_pretty($data, int $extra_flags = 0, int $exclude_flags = 0): string { // prettiest flags for: 7.3.9 $flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | (defined("JSON_UNESCAPED_LINE_TERMINATORS") ? JSON_UNESCAPED_LINE_TERMINATORS : 0) | JSON_PRESERVE_ZERO_FRACTION | (defined("JSON_THROW_ON_ERROR") ? JSON_THROW_ON_ERROR : 0); $flags = ($flags | $extra_flags) & ~ $exclude_flags; return (json_encode($data, $flags)); } function loadHTML_noemptywhitespace(string $html, int $extra_flags = 0, int $exclude_flags = 0): \DOMDocument { $flags = LIBXML_HTML_NODEFDTD | LIBXML_NOBLANKS | LIBXML_NONET; $flags = ($flags & ~ $exclude_flags) | $extra_flags; $domd = new \DOMDocument(); $domd->preserveWhiteSpace = false; @$domd->loadHTML('<?xml encoding="UTF-8">' . $html, $flags); $removeAnnoyingWhitespaceTextNodes = function (\DOMNode $node) use (&$removeAnnoyingWhitespaceTextNodes): void { if ($node->hasChildNodes()) { // Warning: it's important to do it backwards; if you do it forwards, the index for DOMNodeList might become invalidated; // that's why i don't use foreach() - don't change it (unless you know what you're doing, ofc) for ($i = $node->childNodes->length - 1; $i >= 0; --$i) { $removeAnnoyingWhitespaceTextNodes($node->childNodes->item($i)); } } if ($node->nodeType === XML_TEXT_NODE && !$node->hasChildNodes() && !$node->hasAttributes() && ! strlen(trim($node->textContent))) { //echo "Removing annoying POS"; // var_dump($node); $node->parentNode->removeChild($node); } //elseif ($node instanceof DOMText) { echo "not removed"; var_dump($node, $node->hasChildNodes(), $node->hasAttributes(), trim($node->textContent)); } }; $removeAnnoyingWhitespaceTextNodes($domd); return $domd; } $domd=loadHTML_noemptywhitespace($html); $xp=new DOMXPath($domd); $extracted=[]; foreach($xp->query("//div[contains(@class,'singlepost')]/ul") as $ul){ $ulData=[]; foreach($xp->query("./li", $ul) as $li){ $data = explode(":",$li->nodeValue, 2); $uldata[trim($data[0])] = trim($data[1]); } $extracted[]=$uldata; } echo json_encode_pretty($extracted);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 25, Position 2 = 51
Branch analysis from position: 25
2 jumps found. (Code = 78) Position 1 = 26, Position 2 = 51
Branch analysis from position: 26
2 jumps found. (Code = 77) Position 1 = 32, Position 2 = 47
Branch analysis from position: 32
2 jumps found. (Code = 78) Position 1 = 33, Position 2 = 47
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
Branch analysis from position: 47
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 51
filename:       /in/80rua
function name:  (null)
number of ops:  57
compiled vars:  !0 = $html, !1 = $domd, !2 = $xp, !3 = $extracted, !4 = $ul, !5 = $ulData, !6 = $li, !7 = $data, !8 = $uldata
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                                   'ini_set'
          1        SEND_VAL                                                     'display_errors'
          2        SEND_VAL                                                     'On'
          3        DO_ICALL                                                     
    4     4        INIT_FCALL                                                   'header'
          5        SEND_VAL                                                     'Content-Type%3A+text%2Fplain%3Bcharset%3Dutf-8'
          6        DO_ICALL                                                     
          7        INIT_FCALL                                                   'ini_set'
          8        SEND_VAL                                                     'html_errors'
          9        SEND_VAL                                                     '0'
         10        DO_ICALL                                                     
   10    11        ASSIGN                                                       !0, '%3Cdiv+class%3D%22singlepost%22%3E%0A++++%0A%3Cul+class%3D%22linha_status%22+style%3D%22%22%3E%0A%3Cli%3EStatus%3A+%3Cb%3EObjeto+em+tr%C3%A2nsito+-+por+favor+aguarde%3C%2Fb%3E%3C%2Fli%3E%0A%3Cli%3EData++%3A+24%2F10%2F2021+%7C+Hora%3A+12%3A04%3C%2Fli%3E+++++++++++%0A%3Cli%3EOrigem%3A+Unidade+de+Tratamento+-+Jaboatao+Dos+Guararapes+%2F+PE%3C%2Fli%3E%0A%3Cli%3EDestino%3A+Ag%C3%AAncia+dos+Correios+-+Cuitegi+%2F+PB%3C%2Fli%3E%0A%3C%2Ful%3E%0A%0A%3Cul+class%3D%22linha_status%22+style%3D%22%22%3E%0A%3Cli%3EStatus%3A+%3Cb%3EObjeto+em+tr%C3%A2nsito+-+por+favor+aguarde%3C%2Fb%3E%3C%2Fli%3E%0A%3Cli%3EData++%3A+19%2F10%2F2021+%7C+Hora%3A+00%3A03%3C%2Fli%3E+++++++++++%0A%3Cli%3EOrigem%3A+Unidade+de+Log%C3%ADstica+Integrada+-+Curitiba+%2F+PR%3C%2Fli%3E%0A%3Cli%3EDestino%3A+Unidade+de+Tratamento+-+Recife+%2F+PE%3C%2Fli%3E%0A%3C%2Ful%3E%0A%0A%3Cul+class%3D%22linha_status%22+style%3D%22%22%3E%0A%3Cli%3EStatus%3A+%3Cb%3EFiscaliza%C3%A7%C3%A3o+aduaneira+finalizada%3C%2Fb%3E%3C%2Fli%3E%0A%3Cli%3EData++%3A+18%2F10%2F2021+%7C+Hora%3A+23%3A35%3C%2Fli%3E%0A%3Cli%3ELocal%3A+Unidade+Operacional+-+Curitiba+%2F+PR%3C%2Fli%3E%0A%3C%2Ful%3E%0A%0A%3Cul+class%3D%22linha_status%22+style%3D%22%22%3E%0A%3Cli%3EStatus%3A+%3Cb%3EObjeto+recebido+pelos+Correios+do+Brasil%3C%2Fb%3E%3C%2Fli%3E%0A%3Cli%3EData++%3A+16%2F10%2F2021+%7C+Hora%3A+11%3A45%3C%2Fli%3E%0A%3Cli%3ELocal%3A+Unidade+de+Log%C3%ADstica+Integrada+-+Curitiba+%2F+PR%3C%2Fli%3E%0A%3C%2Ful%3E%0A%0A%3Cul+class%3D%22linha_status%22+style%3D%22%22%3E%0A%3Cli%3EStatus%3A+%3Cb%3EObjeto+postado%3C%2Fb%3E%3C%2Fli%3E%0A%3Cli%3EData++%3A+14%2F10%2F2021+%7C+Hora%3A+20%3A30%3C%2Fli%3E%0A%3Cli%3ELocal%3A+Pa%C3%ADs+-++%2F+%3C%2Fli%3E%0A%3C%2Ful%3E%0A%0A%3C%2Fdiv%3E'
   82    12        INIT_FCALL                                                   'loadhtml_noemptywhitespace'
         13        SEND_VAR                                                     !0
         14        DO_FCALL                                          0  $13     
         15        ASSIGN                                                       !1, $13
   83    16        NEW                                                  $15     'DOMXPath'
         17        SEND_VAR_EX                                                  !1
         18        DO_FCALL                                          0          
         19        ASSIGN                                                       !2, $15
   84    20        ASSIGN                                                       !3, <array>
   85    21        INIT_METHOD_CALL                                             !2, 'query'
         22        SEND_VAL_EX                                                  '%2F%2Fdiv%5Bcontains%28%40class%2C%27singlepost%27%29%5D%2Ful'
         23        DO_FCALL                                          0  $19     
         24      > FE_RESET_R                                           $20     $19, ->51
         25    > > FE_FETCH_R                                                   $20, !4, ->51
   86    26    >   ASSIGN                                                       !5, <array>
   87    27        INIT_METHOD_CALL                                             !2, 'query'
         28        SEND_VAL_EX                                                  '.%2Fli'
         29        SEND_VAR_EX                                                  !4
         30        DO_FCALL                                          0  $22     
         31      > FE_RESET_R                                           $23     $22, ->47
         32    > > FE_FETCH_R                                                   $23, !6, ->47
   88    33    >   INIT_FCALL                                                   'explode'
         34        SEND_VAL                                                     '%3A'
         35        FETCH_OBJ_R                                          ~24     !6, 'nodeValue'
         36        SEND_VAL                                                     ~24
         37        SEND_VAL                                                     2
         38        DO_ICALL                                             $25     
         39        ASSIGN                                                       !7, $25
   89    40        FETCH_DIM_R                                          ~27     !7, 0
         41        FRAMELESS_ICALL_1                trim                ~28     ~27
         42        FETCH_DIM_R                                          ~30     !7, 1
         43        FRAMELESS_ICALL_1                trim                ~31     ~30
         44        ASSIGN_DIM                                                   !8, ~28
         45        OP_DATA                                                      ~31
   87    46      > JMP                                                          ->32
         47    >   FE_FREE                                                      $23
   91    48        ASSIGN_DIM                                                   !3
         49        OP_DATA                                                      !8
   85    50      > JMP                                                          ->25
         51    >   FE_FREE                                                      $20
   93    52        INIT_FCALL                                                   'json_encode_pretty'
         53        SEND_VAR                                                     !3
         54        DO_FCALL                                          0  $33     
         55        ECHO                                                         $33
         56      > RETURN                                                       1

Function json_encode_pretty:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
Branch analysis from position: 12
filename:       /in/80rua
function name:  json_encode_pretty
number of ops:  27
compiled vars:  !0 = $data, !1 = $extra_flags, !2 = $exclude_flags, !3 = $flags
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   47     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      0
          2        RECV_INIT                                            !2      0
   50     3      > JMPZ                                                         <true>, ->6
          4    >   QM_ASSIGN                                            ~4      2048
          5      > JMP                                                          ->7
          6    >   QM_ASSIGN                                            ~4      0
          7    >   BW_OR                                                ~5      ~4, 448
          8        BW_OR                                                ~6      ~5, 1024
          9      > JMPZ                                                         <true>, ->12
         10    >   QM_ASSIGN                                            ~7      4194304
         11      > JMP                                                          ->13
         12    >   QM_ASSIGN                                            ~7      0
         13    >   BW_OR                                                ~8      ~6, ~7
         14        ASSIGN                                                       !3, ~8
   51    15        BW_OR                                                ~10     !3, !1
         16        BW_NOT                                               ~11     !2
         17        BW_AND                                               ~12     ~10, ~11
         18        ASSIGN                                                       !3, ~12
   52    19        INIT_FCALL                                                   'json_encode'
         20        SEND_VAR                                                     !0
         21        SEND_VAR                                                     !3
         22        DO_ICALL                                             $14     
         23        VERIFY_RETURN_TYPE                                           $14
         24      > RETURN                                                       $14
   53    25*       VERIFY_RETURN_TYPE                                           
         26*     > RETURN                                                       null

End of function json_encode_pretty

Function loadhtml_noemptywhitespace:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/80rua
function name:  loadHTML_noemptywhitespace
number of ops:  30
compiled vars:  !0 = $html, !1 = $extra_flags, !2 = $exclude_flags, !3 = $flags, !4 = $domd, !5 = $removeAnnoyingWhitespaceTextNodes
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   56     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      0
          2        RECV_INIT                                            !2      0
   58     3        ASSIGN                                                       !3, 2308
   59     4        BW_NOT                                               ~7      !2
          5        BW_AND                                               ~8      !3, ~7
          6        BW_OR                                                ~9      !1, ~8
          7        ASSIGN                                                       !3, ~9
   61     8        NEW                                                  $11     'DOMDocument'
          9        DO_FCALL                                          0          
         10        ASSIGN                                                       !4, $11
   62    11        ASSIGN_OBJ                                                   !4, 'preserveWhiteSpace'
         12        OP_DATA                                                      <false>
   63    13        BEGIN_SILENCE                                        ~15     
         14        INIT_METHOD_CALL                                             !4, 'loadHTML'
         15        CONCAT                                               ~16     '%3C%3Fxml+encoding%3D%22UTF-8%22%3E', !0
         16        SEND_VAL_EX                                                  ~16
         17        SEND_VAR_EX                                                  !3
         18        DO_FCALL                                          0          
         19        END_SILENCE                                                  ~15
   64    20        DECLARE_LAMBDA_FUNCTION                              ~18     [0]
         21        BIND_LEXICAL                                                 ~18, !5
         22        ASSIGN                                                       !5, ~18
   78    23        INIT_DYNAMIC_CALL                                            !5
         24        SEND_VAR_EX                                                  !4
         25        DO_FCALL                                          0          
   79    26        VERIFY_RETURN_TYPE                                           !4
         27      > RETURN                                                       !4
   80    28*       VERIFY_RETURN_TYPE                                           
         29*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 20
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 10
Branch analysis from position: 20
2 jumps found. (Code = 46) Position 1 = 23, Position 2 = 27
Branch analysis from position: 23
2 jumps found. (Code = 46) Position 1 = 28, Position 2 = 32
Branch analysis from position: 28
2 jumps found. (Code = 46) Position 1 = 33, Position 2 = 38
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 43
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 43
Branch analysis from position: 38
Branch analysis from position: 32
Branch analysis from position: 27
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 10
Branch analysis from position: 20
Branch analysis from position: 10
Branch analysis from position: 20
filename:       /in/80rua
function name:  {closure:loadHTML_noemptywhitespace():64}
number of ops:  44
compiled vars:  !0 = $node, !1 = $removeAnnoyingWhitespaceTextNodes, !2 = $i
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   64     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
   65     2        INIT_METHOD_CALL                                             !0, 'hasChildNodes'
          3        DO_FCALL                                          0  $3      
          4      > JMPZ                                                         $3, ->20
   68     5    >   FETCH_OBJ_R                                          ~4      !0, 'childNodes'
          6        FETCH_OBJ_R                                          ~5      ~4, 'length'
          7        SUB                                                  ~6      ~5, 1
          8        ASSIGN                                                       !2, ~6
          9      > JMP                                                          ->18
   69    10    >   INIT_DYNAMIC_CALL                                            !1
         11        FETCH_OBJ_R                                          ~8      !0, 'childNodes'
         12        INIT_METHOD_CALL                                             ~8, 'item'
         13        SEND_VAR_EX                                                  !2
         14        DO_FCALL                                          0  $9      
         15        SEND_VAR_NO_REF_EX                                           $9
         16        DO_FCALL                                          0          
   68    17        PRE_DEC                                                      !2
         18    >   IS_SMALLER_OR_EQUAL                                          0, !2
         19      > JMPNZ                                                        ~12, ->10
   72    20    >   FETCH_OBJ_R                                          ~13     !0, 'nodeType'
         21        IS_IDENTICAL                                         ~14     ~13, 3
         22      > JMPZ_EX                                              ~14     ~14, ->27
         23    >   INIT_METHOD_CALL                                             !0, 'hasChildNodes'
         24        DO_FCALL                                          0  $15     
         25        BOOL_NOT                                             ~16     $15
         26        BOOL                                                 ~14     ~16
         27    > > JMPZ_EX                                              ~14     ~14, ->32
         28    >   INIT_METHOD_CALL                                             !0, 'hasAttributes'
         29        DO_FCALL                                          0  $17     
         30        BOOL_NOT                                             ~18     $17
         31        BOOL                                                 ~14     ~18
         32    > > JMPZ_EX                                              ~14     ~14, ->38
         33    >   FETCH_OBJ_R                                          ~19     !0, 'textContent'
         34        FRAMELESS_ICALL_1                trim                ~20     ~19
         35        STRLEN                                               ~21     ~20
         36        BOOL_NOT                                             ~22     ~21
         37        BOOL                                                 ~14     ~22
         38    > > JMPZ                                                         ~14, ->43
   75    39    >   FETCH_OBJ_R                                          ~23     !0, 'parentNode'
         40        INIT_METHOD_CALL                                             ~23, 'removeChild'
         41        SEND_VAR_EX                                                  !0
         42        DO_FCALL                                          0          
   77    43    > > RETURN                                                       null

End of Dynamic Function 0

End of function loadhtml_noemptywhitespace

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
139.78 ms | 1638 KiB | 19 Q