3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* * Some sophisticated tests against PHP's mb_detect_encoding() function, * testing ASCII, ISO-8859-1, Windows-1252, UTF-8 and UTF-16 in both byte orders. * * Tim Weber, 2014-07-04 */ $samples = array( 'ASCII only' => array( 'string' => 'Hallo', 'valid' => 'A 1252 L1 U8', // invalid in UTF-16: odd number of bytes ), 'ASCII with null in the middle' => array( 'string' => "Hal\x00lo.", 'valid' => 'A 1252 L1 U8', // invalid in UTF-16: odd number ), '8-bit string with character not in Windows-1252' => array( 'string' => "Hal\x81lo", // 0x80 and 0x82 are used in 1252, 0x81 not 'valid' => 'L1 U16B U16L', // valid in UTF-16 ), 'UTF-8 with a 2-byte BMP character' => array( 'string' => "Hall\xc3\xb6.", // Hallö. 'valid' => '1252 L1 U8', ), 'UTF-8 with a 3-byte BMP character' => array( 'string' => "Hal\xef\xbb\xbflo.", // Hal<U+FEFF>lo. 'valid' => '1252 L1 U8', ), 'UTF-8 with a 4-byte SP character' => array( 'string' => "Oh \xf0\x9f\x92\xa9!", // Oh <F+1F4A9 (pile of poo)>! 'valid' => '1252 L1 U8 U16B U16L', ), 'UTF-8 with a really high PUA-B character' => array( 'string' => "(\xf4\x8f\xbf\xba)", // (<U+10FFFA>) 'valid' => 'L1 U8 U16B U16L', // Invalid in 1252 b/c of 0x8f ), 'single surrogate lead in UTF-8' => array( 'string' => "\xed\xa0\x82", // (<U+D802>) 'valid' => '1252 L1', ), 'single surrogate lead in UTF-16BE' => array( 'string' => "\xd8\x02", // (<U+D802>) 'valid' => '1252 L1 U16L', ), 'single surrogate trail in UTF-8' => array( 'string' => "\xed\xb0\x82", // (<U+DC02>) 'valid' => '1252 L1', ), 'single surrogate trail in UTF-16LE' => array( 'string' => "\x02\xdc", // (<U+DC02>) 'valid' => '1252 L1 U16B', ), 'Plane 17 UTF-8 character' => array( 'string' => "\xf4\x92\x8d\x85", // (<U+11234>) 'valid' => 'L1 U16B U16L', // Invalid in 1252 b/c of 0x8d ), 'null character encoded in two UTF-8 bytes' => array( 'string' => "\xc0\x80", // <U+0000> 'valid' => '1252 L1 U16B U16L', ), 'UTF-8 with a BMP non-shortest sequence (3 instead of 2 bytes)' => array( 'string' => "Hall\xe0\x83\xb6.", // Hallö. 'valid' => '1252 L1 U16B U16L', ), 'UTF-16BE of an ASCII character' => array( 'string' => "\x00\x79", // y 'valid' => 'ASCII 1252 L1 U8 U16B U16L', ), 'UTF-16LE of an SMP character' => array( 'string' => "\x34\xd8\x1e\xdd", // <U+1D11E (musical symbol g clef)> 'valid' => '1252 L1 U16B U16L', ), 'UTF-16BE BOM' => array( 'string' => "\xfe\xff", // <U+FEFF> 'valid' => '1252 L1 U16B U16L', // U+FFFE is _not_ an invalid codepoint, therefore valid U16L ), 'UTF-16LE SP character' => array( 'string' => "\x3d\xd8\xa9\xdc", // <U+1F4A9> 'valid' => '1252 L1 U16L U16B', ), 'UTF-16BE SP character that is lone surrogate lead in UTF-16LE' => array( 'string' => "\xd8\x34\xdd\xd8", // <U+1D1D8 (musical symbol torculus> 'valid' => '1252 L1 U16B', ), ); $encoding_mappings = array( 'ASCII' => 'A', 'Windows-1252' => '1252', 'ISO-8859-1' => 'L1', 'UTF-8' => 'U8', 'UTF-16BE' => 'U16B', 'UTF-16LE' => 'U16L', ); $encodings = array_keys($encoding_mappings); foreach ($samples as $name => &$data) { // Convert string "valid" to array. if (is_string($data['valid'])) { $data['valid'] = explode(' ', $data['valid']); } // Convert abbreviated encodings to full name. foreach ($data['valid'] as &$short) { $found = array_search($short, $encoding_mappings); if ($found !== false) { $short = $found; } } unset($short); // Remove reference. // Check validity. foreach ($encodings as $encoding) { $valid = mb_check_encoding($data['string'], $encoding); $expected = array_search($encoding, $data['valid']) !== false; if ($valid != $expected) { printf("\"%s\" should be %s in %s, but is detected as %s\n", $name, $expected ? 'valid' : 'invalid', $encoding, $valid ? 'valid' : 'invalid' ); } } }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 7, Position 2 = 68
Branch analysis from position: 7
2 jumps found. (Code = 126) Position 1 = 8, Position 2 = 68
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 19
Branch analysis from position: 12
2 jumps found. (Code = 125) Position 1 = 21, Position 2 = 31
Branch analysis from position: 21
2 jumps found. (Code = 126) Position 1 = 22, Position 2 = 31
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 30
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
Branch analysis from position: 30
Branch analysis from position: 31
2 jumps found. (Code = 77) Position 1 = 34, Position 2 = 66
Branch analysis from position: 34
2 jumps found. (Code = 78) Position 1 = 35, Position 2 = 66
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 65
Branch analysis from position: 50
2 jumps found. (Code = 43) Position 1 = 54, Position 2 = 56
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 57
Branch analysis from position: 57
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 62
Branch analysis from position: 60
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 62
Branch analysis from position: 60
Branch analysis from position: 62
Branch analysis from position: 65
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 66
Branch analysis from position: 31
Branch analysis from position: 19
Branch analysis from position: 68
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 68
filename:       /in/ucFuq
function name:  (null)
number of ops:  70
compiled vars:  !0 = $samples, !1 = $encoding_mappings, !2 = $encodings, !3 = $data, !4 = $name, !5 = $short, !6 = $found, !7 = $encoding, !8 = $valid, !9 = $expected
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   ASSIGN                                                   !0, <array>
   89     1        ASSIGN                                                   !1, <array>
   97     2        INIT_FCALL                                               'array_keys'
          3        SEND_VAR                                                 !1
          4        DO_ICALL                                         $12     
          5        ASSIGN                                                   !2, $12
   99     6      > FE_RESET_RW                                      $14     !0, ->68
          7    > > FE_FETCH_RW                                      ~15     $14, !3, ->68
          8    >   ASSIGN                                                   !4, ~15
  101     9        FETCH_DIM_R                                      ~17     !3, 'valid'
         10        TYPE_CHECK                                   64          ~17
         11      > JMPZ                                                     ~18, ->19
  102    12    >   INIT_FCALL                                               'explode'
         13        SEND_VAL                                                 '+'
         14        FETCH_DIM_R                                      ~20     !3, 'valid'
         15        SEND_VAL                                                 ~20
         16        DO_ICALL                                         $21     
         17        ASSIGN_DIM                                               !3, 'valid'
         18        OP_DATA                                                  $21
  105    19    >   FETCH_DIM_W                                      $22     !3, 'valid'
         20      > FE_RESET_RW                                      $23     $22, ->31
         21    > > FE_FETCH_RW                                              $23, !5, ->31
  106    22    >   INIT_FCALL                                               'array_search'
         23        SEND_VAR                                                 !5
         24        SEND_VAR                                                 !1
         25        DO_ICALL                                         $24     
         26        ASSIGN                                                   !6, $24
  107    27        TYPE_CHECK                                  1018          !6
         28      > JMPZ                                                     ~26, ->30
  108    29    >   ASSIGN                                                   !5, !6
  105    30    > > JMP                                                      ->21
         31    >   FE_FREE                                                  $23
  111    32        UNSET_CV                                                 !5
  113    33      > FE_RESET_R                                       $28     !2, ->66
         34    > > FE_FETCH_R                                               $28, !7, ->66
  114    35    >   INIT_FCALL                                               'mb_check_encoding'
         36        FETCH_DIM_R                                      ~29     !3, 'string'
         37        SEND_VAL                                                 ~29
         38        SEND_VAR                                                 !7
         39        DO_ICALL                                         $30     
         40        ASSIGN                                                   !8, $30
  115    41        INIT_FCALL                                               'array_search'
         42        SEND_VAR                                                 !7
         43        FETCH_DIM_R                                      ~32     !3, 'valid'
         44        SEND_VAL                                                 ~32
         45        DO_ICALL                                         $33     
         46        TYPE_CHECK                                  1018  ~34     $33
         47        ASSIGN                                                   !9, ~34
  116    48        IS_NOT_EQUAL                                             !8, !9
         49      > JMPZ                                                     ~36, ->65
  117    50    >   INIT_FCALL                                               'printf'
         51        SEND_VAL                                                 '%22%25s%22+should+be+%25s+in+%25s%2C+but+is+detected+as+%25s%0A'
  118    52        SEND_VAR                                                 !4
  119    53      > JMPZ                                                     !9, ->56
         54    >   QM_ASSIGN                                        ~37     'valid'
         55      > JMP                                                      ->57
         56    >   QM_ASSIGN                                        ~37     'invalid'
         57    >   SEND_VAL                                                 ~37
  120    58        SEND_VAR                                                 !7
  121    59      > JMPZ                                                     !8, ->62
         60    >   QM_ASSIGN                                        ~38     'valid'
         61      > JMP                                                      ->63
         62    >   QM_ASSIGN                                        ~38     'invalid'
         63    >   SEND_VAL                                                 ~38
         64        DO_ICALL                                                 
  113    65    > > JMP                                                      ->34
         66    >   FE_FREE                                                  $28
   99    67      > JMP                                                      ->7
         68    >   FE_FREE                                                  $14
  125    69      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
165.47 ms | 1404 KiB | 23 Q