3v4l.org

run code in 500+ PHP versions simultaneously
<?php // Differences between approaches to handling utf-8 encoding with DomDocument function print_it( $version, $output ) { echo "$version:\n"; echo "---\n"; echo $output; echo "---\n\n"; } $html = ' <p data-image-description="&lt;p&gt;Test!&lt;/p&gt;">Test</p> <p>äöü</p> <p>ß</p> <p>系の家庭に</p> <p>Example &lt;p&gt;Test!&lt;/p&gt;</p> '; print_it("Original", $html); $dom = new DOMDocument('1.0', 'UTF-8'); $dom->loadHTML( $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ); print_it( "Mojibake", $dom->saveHTML() ); print_it( "Mojibake as you would see", mb_convert_encoding($dom->saveHTML(), "UTF-8", "HTML-ENTITIES") ); $dom = new DOMDocument('1.0', 'UTF-8'); $dom->loadHTML( mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8' ), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ); print_it("mb_convert_encoding", mb_convert_encoding( $dom->saveHTML(), 'UTF-8', 'HTML-ENTITIES' ) ); $dom = new DOMDocument('1.0', 'UTF-8'); $dom->loadHTML('<?xml version="1.0" encoding="utf-8"?>' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ); print_it( "<?xml encoding", str_replace('<?xml version="1.0" encoding="utf-8"?>', '', $dom->saveHTML() ) ); $dom = new DOMDocument('1.0', 'UTF-8'); $dom->loadHTML( '<html><head><meta charset="utf-8"></head><body>' . $html . '</body></html>', LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ); print_it( "<meta charset", preg_replace( array( '/^<html><head><meta charset="utf-8"><\/head><body>/', '/<\/body><\/html>$/' ), '', $dom->saveHTML() ) ); $dom = new DOMDocument('1.0', 'UTF-8'); $dom->loadHTML( '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body>' . $html . '</body></html>', LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ); print_it( "<meta content-type charset", preg_replace( array( '/^<html><head><meta http-equiv="Content-Type" content="text\/html; charset=utf-8"><\/head><body>/', '/<\/body><\/html>$/' ), '', $dom->saveHTML() ) );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EaZv9
function name:  (null)
number of ops:  113
compiled vars:  !0 = $html, !1 = $dom
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   12     0  E >   ASSIGN                                                       !0, '%0A%09%3Cp+data-image-description%3D%22%26lt%3Bp%26gt%3BTest%21%26lt%3B%2Fp%26gt%3B%22%3ETest%3C%2Fp%3E%0A%09%3Cp%3E%C3%A4%C3%B6%C3%BC%3C%2Fp%3E%0A%09%3Cp%3E%C3%9F%3C%2Fp%3E%0A%09%3Cp%3E%E7%B3%BB%E3%81%AE%E5%AE%B6%E5%BA%AD%E3%81%AB%3C%2Fp%3E%0A%09%3Cp%3EExample+%26lt%3Bp%26gt%3BTest%21%26lt%3B%2Fp%26gt%3B%3C%2Fp%3E%0A'
   20     1        INIT_FCALL                                                   'print_it'
          2        SEND_VAL                                                     'Original'
          3        SEND_VAR                                                     !0
          4        DO_FCALL                                          0          
   22     5        NEW                                                  $4      'DOMDocument'
          6        SEND_VAL_EX                                                  '1.0'
          7        SEND_VAL_EX                                                  'UTF-8'
          8        DO_FCALL                                          0          
          9        ASSIGN                                                       !1, $4
   23    10        INIT_METHOD_CALL                                             !1, 'loadHTML'
         11        SEND_VAR_EX                                                  !0
         12        SEND_VAL_EX                                                  73732
         13        DO_FCALL                                          0          
   24    14        INIT_FCALL                                                   'print_it'
         15        SEND_VAL                                                     'Mojibake'
         16        INIT_METHOD_CALL                                             !1, 'saveHTML'
         17        DO_FCALL                                          0  $8      
         18        SEND_VAR                                                     $8
         19        DO_FCALL                                          0          
   25    20        INIT_FCALL                                                   'print_it'
         21        SEND_VAL                                                     'Mojibake+as+you+would+see'
         22        INIT_FCALL                                                   'mb_convert_encoding'
         23        INIT_METHOD_CALL                                             !1, 'saveHTML'
         24        DO_FCALL                                          0  $10     
         25        SEND_VAR                                                     $10
         26        SEND_VAL                                                     'UTF-8'
         27        SEND_VAL                                                     'HTML-ENTITIES'
         28        DO_ICALL                                             $11     
         29        SEND_VAR                                                     $11
         30        DO_FCALL                                          0          
   27    31        NEW                                                  $13     'DOMDocument'
         32        SEND_VAL_EX                                                  '1.0'
         33        SEND_VAL_EX                                                  'UTF-8'
         34        DO_FCALL                                          0          
         35        ASSIGN                                                       !1, $13
   28    36        INIT_METHOD_CALL                                             !1, 'loadHTML'
         37        INIT_FCALL                                                   'mb_convert_encoding'
         38        SEND_VAR                                                     !0
         39        SEND_VAL                                                     'HTML-ENTITIES'
         40        SEND_VAL                                                     'UTF-8'
         41        DO_ICALL                                             $16     
         42        SEND_VAR_NO_REF_EX                                           $16
         43        SEND_VAL_EX                                                  73732
         44        DO_FCALL                                          0          
   29    45        INIT_FCALL                                                   'print_it'
         46        SEND_VAL                                                     'mb_convert_encoding'
         47        INIT_FCALL                                                   'mb_convert_encoding'
         48        INIT_METHOD_CALL                                             !1, 'saveHTML'
         49        DO_FCALL                                          0  $18     
         50        SEND_VAR                                                     $18
         51        SEND_VAL                                                     'UTF-8'
         52        SEND_VAL                                                     'HTML-ENTITIES'
         53        DO_ICALL                                             $19     
         54        SEND_VAR                                                     $19
         55        DO_FCALL                                          0          
   32    56        NEW                                                  $21     'DOMDocument'
         57        SEND_VAL_EX                                                  '1.0'
         58        SEND_VAL_EX                                                  'UTF-8'
         59        DO_FCALL                                          0          
         60        ASSIGN                                                       !1, $21
   33    61        INIT_METHOD_CALL                                             !1, 'loadHTML'
         62        CONCAT                                               ~24     '%3C%3Fxml+version%3D%221.0%22+encoding%3D%22utf-8%22%3F%3E', !0
         63        SEND_VAL_EX                                                  ~24
         64        SEND_VAL_EX                                                  73732
         65        DO_FCALL                                          0          
   34    66        INIT_FCALL                                                   'print_it'
         67        SEND_VAL                                                     '%3C%3Fxml+encoding'
         68        INIT_METHOD_CALL                                             !1, 'saveHTML'
         69        DO_FCALL                                          0  $26     
         70        FRAMELESS_ICALL_3                str_replace         ~27     '%3C%3Fxml+version%3D%221.0%22+encoding%3D%22utf-8%22%3F%3E', ''
         71        OP_DATA                                                      $26
         72        SEND_VAL                                                     ~27
         73        DO_FCALL                                          0          
   36    74        NEW                                                  $29     'DOMDocument'
         75        SEND_VAL_EX                                                  '1.0'
         76        SEND_VAL_EX                                                  'UTF-8'
         77        DO_FCALL                                          0          
         78        ASSIGN                                                       !1, $29
   37    79        INIT_METHOD_CALL                                             !1, 'loadHTML'
         80        CONCAT                                               ~32     '%3Chtml%3E%3Chead%3E%3Cmeta+charset%3D%22utf-8%22%3E%3C%2Fhead%3E%3Cbody%3E', !0
         81        CONCAT                                               ~33     ~32, '%3C%2Fbody%3E%3C%2Fhtml%3E'
         82        SEND_VAL_EX                                                  ~33
         83        SEND_VAL_EX                                                  65540
         84        DO_FCALL                                          0          
   38    85        INIT_FCALL                                                   'print_it'
         86        SEND_VAL                                                     '%3Cmeta+charset'
         87        INIT_METHOD_CALL                                             !1, 'saveHTML'
         88        DO_FCALL                                          0  $35     
         89        FRAMELESS_ICALL_3                preg_replace        ~36     <array>, ''
         90        OP_DATA                                                      $35
         91        SEND_VAL                                                     ~36
         92        DO_FCALL                                          0          
   40    93        NEW                                                  $38     'DOMDocument'
         94        SEND_VAL_EX                                                  '1.0'
         95        SEND_VAL_EX                                                  'UTF-8'
         96        DO_FCALL                                          0          
         97        ASSIGN                                                       !1, $38
   41    98        INIT_METHOD_CALL                                             !1, 'loadHTML'
         99        CONCAT                                               ~41     '%3Chtml%3E%3Chead%3E%3Cmeta+http-equiv%3D%22Content-Type%22+content%3D%22text%2Fhtml%3B+charset%3Dutf-8%22%3E%3C%2Fhead%3E%3Cbody%3E', !0
        100        CONCAT                                               ~42     ~41, '%3C%2Fbody%3E%3C%2Fhtml%3E'
        101        SEND_VAL_EX                                                  ~42
        102        SEND_VAL_EX                                                  65540
        103        DO_FCALL                                          0          
   42   104        INIT_FCALL                                                   'print_it'
        105        SEND_VAL                                                     '%3Cmeta+content-type+charset'
        106        INIT_METHOD_CALL                                             !1, 'saveHTML'
        107        DO_FCALL                                          0  $44     
        108        FRAMELESS_ICALL_3                preg_replace        ~45     <array>, ''
        109        OP_DATA                                                      $44
        110        SEND_VAL                                                     ~45
        111        DO_FCALL                                          0          
        112      > RETURN                                                       1

Function print_it:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EaZv9
function name:  print_it
number of ops:  9
compiled vars:  !0 = $version, !1 = $output
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    5     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
    6     2        NOP                                                          
          3        FAST_CONCAT                                          ~2      !0, '%3A%0A'
          4        ECHO                                                         ~2
    7     5        ECHO                                                         '---%0A'
    8     6        ECHO                                                         !1
    9     7        ECHO                                                         '---%0A%0A'
   10     8      > RETURN                                                       null

End of function print_it

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
117.59 ms | 503 KiB | 17 Q