3v4l.org

run code in 300+ PHP versions simultaneously
<?php $original_string = '<p>some text&nbsp;<img src="data:image/png;base64,randomIMGData" data-filename="file1.png" style="width: 120px;"><img src="data:image/jpeg;base64,moreIMGData" data-filename="file2.jpg" style="width: 120px;"><p>Some other text<p><img alt="Regular image" src="http://example.com/"><img src="data:image/png;base64,invalidElement" style="width: 120px;"></p>'; $doc = new DOMDocument(); // Treat the string as a frament and do not add a DOCTYPE or mandatory tags $doc->loadHTML($original_string, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); // Get all images $imgs = $doc->getElementsByTagName('img'); $id_noveho_clanku = 'testdir'; foreach ($imgs as $img) { $src = $img->getAttribute('src'); $name = $img->getAttribute('data-filename'); // Check if it contains base64 data and a filename if (false !== strpos($src, 'data:image') && "" !== $img->getAttribute('data-filename')) { $data = substr($src, strpos($src, ',') + 1); // Data start $img->setAttribute('src', str_replace($data, '', $src)); // Remove data $name = substr($name, 0, strrpos($name, '.')); // Save the image to disk $path = "../img/clanky/${id_noveho_clanku}/${name}.txt"; // file_put_contents ($path, $data); echo "Saving image data: '${data}' to ${path}\n"; } } // Get the HTML with the data removed $parsed = $doc->saveHTML($doc); echo $parsed;
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 77
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 77
Branch analysis from position: 15
2 jumps found. (Code = 46) Position 1 = 29, Position 2 = 34
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 76
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 76
Branch analysis from position: 34
Branch analysis from position: 77
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 77
filename:       /in/CXKkH
function name:  (null)
number of ops:  84
compiled vars:  !0 = $original_string, !1 = $doc, !2 = $imgs, !3 = $id_noveho_clanku, !4 = $img, !5 = $src, !6 = $name, !7 = $data, !8 = $path, !9 = $parsed
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%3Cp%3Esome+text%26nbsp%3B%3Cimg+src%3D%22data%3Aimage%2Fpng%3Bbase64%2CrandomIMGData%22+data-filename%3D%22file1.png%22+style%3D%22width%3A+120px%3B%22%3E%3Cimg+src%3D%22data%3Aimage%2Fjpeg%3Bbase64%2CmoreIMGData%22+data-filename%3D%22file2.jpg%22+style%3D%22width%3A+120px%3B%22%3E%3Cp%3ESome+other+text%3Cp%3E%3Cimg+alt%3D%22Regular+image%22+src%3D%22http%3A%2F%2Fexample.com%2F%22%3E%3Cimg+src%3D%22data%3Aimage%2Fpng%3Bbase64%2CinvalidElement%22+style%3D%22width%3A+120px%3B%22%3E%3C%2Fp%3E'
    4     1        NEW                                              $11     'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $11
    6     4        INIT_METHOD_CALL                                         !1, 'loadHTML'
          5        SEND_VAR_EX                                              !0
          6        SEND_VAL_EX                                              8196
          7        DO_FCALL                                      0          
    9     8        INIT_METHOD_CALL                                         !1, 'getElementsByTagName'
          9        SEND_VAL_EX                                              'img'
         10        DO_FCALL                                      0  $15     
         11        ASSIGN                                                   !2, $15
   11    12        ASSIGN                                                   !3, 'testdir'
   13    13      > FE_RESET_R                                       $18     !2, ->77
         14    > > FE_FETCH_R                                               $18, !4, ->77
   14    15    >   INIT_METHOD_CALL                                         !4, 'getAttribute'
         16        SEND_VAL_EX                                              'src'
         17        DO_FCALL                                      0  $19     
         18        ASSIGN                                                   !5, $19
   15    19        INIT_METHOD_CALL                                         !4, 'getAttribute'
         20        SEND_VAL_EX                                              'data-filename'
         21        DO_FCALL                                      0  $21     
         22        ASSIGN                                                   !6, $21
   18    23        INIT_FCALL                                               'strpos'
         24        SEND_VAR                                                 !5
         25        SEND_VAL                                                 'data%3Aimage'
         26        DO_ICALL                                         $23     
         27        TYPE_CHECK                                  1018  ~24     $23
         28      > JMPZ_EX                                          ~24     ~24, ->34
         29    >   INIT_METHOD_CALL                                         !4, 'getAttribute'
         30        SEND_VAL_EX                                              'data-filename'
         31        DO_FCALL                                      0  $25     
         32        IS_NOT_IDENTICAL                                 ~26     $25, ''
         33        BOOL                                             ~24     ~26
         34    > > JMPZ                                                     ~24, ->76
   19    35    >   INIT_FCALL                                               'substr'
         36        SEND_VAR                                                 !5
         37        INIT_FCALL                                               'strpos'
         38        SEND_VAR                                                 !5
         39        SEND_VAL                                                 '%2C'
         40        DO_ICALL                                         $27     
         41        ADD                                              ~28     $27, 1
         42        SEND_VAL                                                 ~28
         43        DO_ICALL                                         $29     
         44        ASSIGN                                                   !7, $29
   20    45        INIT_METHOD_CALL                                         !4, 'setAttribute'
         46        SEND_VAL_EX                                              'src'
         47        INIT_FCALL                                               'str_replace'
         48        SEND_VAR                                                 !7
         49        SEND_VAL                                                 ''
         50        SEND_VAR                                                 !5
         51        DO_ICALL                                         $31     
         52        SEND_VAR_NO_REF_EX                                       $31
         53        DO_FCALL                                      0          
   22    54        INIT_FCALL                                               'substr'
         55        SEND_VAR                                                 !6
         56        SEND_VAL                                                 0
         57        INIT_FCALL                                               'strrpos'
         58        SEND_VAR                                                 !6
         59        SEND_VAL                                                 '.'
         60        DO_ICALL                                         $33     
         61        SEND_VAR                                                 $33
         62        DO_ICALL                                         $34     
         63        ASSIGN                                                   !6, $34
   24    64        ROPE_INIT                                     5  ~37     '..%2Fimg%2Fclanky%2F'
         65        ROPE_ADD                                      1  ~37     ~37, !3
         66        ROPE_ADD                                      2  ~37     ~37, '%2F'
         67        ROPE_ADD                                      3  ~37     ~37, !6
         68        ROPE_END                                      4  ~36     ~37, '.txt'
         69        ASSIGN                                                   !8, ~36
   26    70        ROPE_INIT                                     5  ~42     'Saving+image+data%3A+%27'
         71        ROPE_ADD                                      1  ~42     ~42, !7
         72        ROPE_ADD                                      2  ~42     ~42, '%27+to+'
         73        ROPE_ADD                                      3  ~42     ~42, !8
         74        ROPE_END                                      4  ~41     ~42, '%0A'
         75        ECHO                                                     ~41
   13    76    > > JMP                                                      ->14
         77    >   FE_FREE                                                  $18
   31    78        INIT_METHOD_CALL                                         !1, 'saveHTML'
         79        SEND_VAR_EX                                              !1
         80        DO_FCALL                                      0  $45     
         81        ASSIGN                                                   !9, $45
   33    82        ECHO                                                     !9
   34    83      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.17 ms | 1022 KiB | 17 Q