3v4l.org

run code in 500+ PHP versions simultaneously
<?php function sanitize_hex_color( $color, $maybe_alpha = false ) { if ( '' === $color ) { return ''; } $allowed_lengths = $maybe_alpha ? array( 4, 5, 7, 9 ) : array( 4, 7 ); $correct_length = in_array( strlen( $color ), $allowed_lengths, true ); if ( $correct_length && preg_replace( '|^#([^A-Fa-f0-9]+)$|', '', $color ) === $color ) { return $color; } } /** * Test Suite */ $happy_bc_tests = array( '$maybe_alpha = false, 3 digit' => array( 'color' => '#123', 'expected' => '#123', 'maybe_alpha' => false, ), '$maybe_alpha = false, 3 letter' => array( 'color' => '#abc', 'expected' => '#abc', 'maybe_alpha' => false, ), '$maybe_alpha = false, 3 mixed' => array( 'color' => '#0ab', 'expected' => '#0ab', 'maybe_alpha' => false, ), '$maybe_alpha = false, 6 digit' => array( 'color' => '#123456', 'expected' => '#123456', 'maybe_alpha' => false, ), '$maybe_alpha = false, 6 letter' => array( 'color' => '#abcdef', 'expected' => '#abcdef', 'maybe_alpha' => false, ), '$maybe_alpha = false, 6 mixed' => array( 'color' => '#abc123', 'expected' => '#abc123', 'maybe_alpha' => false, ), ); $unhappy_bc_tests = array( 'empty string' => array( 'color' => '', 'expected' => '', 'maybe_alpha' => false, ), 'no hash' => array( 'color' => '123', 'expected' => null, 'maybe_alpha' => false, ), 'not a-f' => array( 'color' => '#hjg', 'expected' => null, 'maybe_alpha' => false, ), 'not A-F' => array( 'color' => '#HJG', 'expected' => null, 'maybe_alpha' => false, ), '$maybe_alpha = false, 3 digit with 1 alpha' => array( 'color' => '#123f', 'expected' => null, 'maybe_alpha' => false, ), '$maybe_alpha = false, 3 letter with 1 alpha' => array( 'color' => '#abcf', 'expected' => null, 'maybe_alpha' => false, ), '$maybe_alpha = false, 3 mixed with 1 alpha' => array( 'color' => '#0abf', 'expected' => null, 'maybe_alpha' => false, ), '$maybe_alpha = false, 6 digit with 2 alpha' => array( 'color' => '#123456ff', 'expected' => null, 'maybe_alpha' => false, ), '$maybe_alpha = false, 6 letter with 2 alpha' => array( 'color' => '#abcdefff', 'expected' => null, 'maybe_alpha' => false, ), '$maybe_alpha = false, 6 mixed with 2 alpha' => array( 'color' => '#abc123ff', 'expected' => null, 'maybe_alpha' => false, ), ); $happy_alpha_tests = array( // Happy. '$maybe_alpha = true, 3 digit' => array( 'color' => '#123', 'expected' => '#123', 'maybe_alpha' => true, ), '$maybe_alpha = true, 3 letter' => array( 'color' => '#abc', 'expected' => '#abc', 'maybe_alpha' => true, ), '$maybe_alpha = true, 3 mixed' => array( 'color' => '#0ab', 'expected' => '#0ab', 'maybe_alpha' => true, ), '$maybe_alpha = true, 6 digit' => array( 'color' => '#123456', 'expected' => '#123456', 'maybe_alpha' => true, ), '$maybe_alpha = true, 6 letter' => array( 'color' => '#abcdef', 'expected' => '#abcdef', 'maybe_alpha' => true, ), '$maybe_alpha = true, 6 mixed' => array( 'color' => '#abc123', 'expected' => '#abc123', 'maybe_alpha' => true, ), '$maybe_alpha = true, 3 digit with 1 alpha' => array( 'color' => '#123f', 'expected' => '#123f', 'maybe_alpha' => true, ), '$maybe_alpha = true, 3 letter with 1 alpha' => array( 'color' => '#abcf', 'expected' => '#abcf', 'maybe_alpha' => true, ), '$maybe_alpha = true, 3 mixed with 1 alpha' => array( 'color' => '#0abf', 'expected' => '#0abf', 'maybe_alpha' => true, ), '$maybe_alpha = true, 6 digit with 2 alpha' => array( 'color' => '#123456ff', 'expected' => '#123456ff', 'maybe_alpha' => true, ), '$maybe_alpha = true, 6 letter with 2 alpha' => array( 'color' => '#abcdefff', 'expected' => '#abcdefff', 'maybe_alpha' => true, ), '$maybe_alpha = true, 6 mixed with 2 alpha' => array( 'color' => '#abc123ff', 'expected' => '#abc123ff', 'maybe_alpha' => true, ), ); $unhappy_alpha_tests = array( 'empty string' => array( 'color' => '', 'expected' => '', 'maybe_alpha' => false, ), 'no hash' => array( 'color' => '123', 'expected' => null, 'maybe_alpha' => false, ), 'not a-f' => array( 'color' => '#hjg', 'expected' => null, 'maybe_alpha' => false, ), 'not A-F' => array( 'color' => '#HJG', 'expected' => null, 'maybe_alpha' => false, ), '$maybe_alpha = true, 3 digit with 2 alpha' => array( 'color' => '#123ff', 'expected' => null, 'maybe_alpha' => true, ), '$maybe_alpha = true, 3 letter with 2 alpha' => array( 'color' => '#abcff', 'expected' => null, 'maybe_alpha' => true, ), '$maybe_alpha = true, 3 mixed with 2 alpha' => array( 'color' => '#0abff', 'expected' => null, 'maybe_alpha' => true, ), '$maybe_alpha = true, 6 digit with 1 alpha' => array( 'color' => '#123456f', 'expected' => null, 'maybe_alpha' => true, ), '$maybe_alpha = true, 6 letter with alpha' => array( 'color' => '#abcff', 'expected' => null, 'maybe_alpha' => true, ), '$maybe_alpha = true, 6 mixed with alpha' => array( 'color' => '#0abff', 'expected' => null, 'maybe_alpha' => true, ), ); function run_tests( $tests = array(), $testdox = false ) { $markers = array( $testdox ? '❌' : 'F', $testdox ? '✅' : '.', ); foreach ( $tests as $test => $data ) { list( $color, $expected, $maybe_alpha ) = array_values( $data ); $actual = sanitize_hex_color( $color, $maybe_alpha ); echo $markers[ (int) ( $actual === $expected ) ] . ( $testdox ? " $test" : '' ) . ( $testdox ? PHP_EOL : '' ); } } echo "Happy BC tests\n"; run_tests( $happy_bc_tests, true ); echo "\nUnhappy BC tests\n"; run_tests( $unhappy_bc_tests, true ); echo "\nHappy alpha tests\n"; run_tests( $happy_alpha_tests, true ); echo "\nUnhappy alpha tests\n"; run_tests( $unhappy_alpha_tests, true );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4s5Q6
function name:  (null)
number of ops:  25
compiled vars:  !0 = $happy_bc_tests, !1 = $unhappy_bc_tests, !2 = $happy_alpha_tests, !3 = $unhappy_alpha_tests
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   19     0  E >   ASSIGN                                                       !0, <array>
   52     1        ASSIGN                                                       !1, <array>
  105     2        ASSIGN                                                       !2, <array>
  169     3        ASSIGN                                                       !3, <array>
  237     4        ECHO                                                         'Happy+BC+tests%0A'
  238     5        INIT_FCALL                                                   'run_tests'
          6        SEND_VAR                                                     !0
          7        SEND_VAL                                                     <true>
          8        DO_FCALL                                          0          
  240     9        ECHO                                                         '%0AUnhappy+BC+tests%0A'
  241    10        INIT_FCALL                                                   'run_tests'
         11        SEND_VAR                                                     !1
         12        SEND_VAL                                                     <true>
         13        DO_FCALL                                          0          
  243    14        ECHO                                                         '%0AHappy+alpha+tests%0A'
  244    15        INIT_FCALL                                                   'run_tests'
         16        SEND_VAR                                                     !2
         17        SEND_VAL                                                     <true>
         18        DO_FCALL                                          0          
  246    19        ECHO                                                         '%0AUnhappy+alpha+tests%0A'
  247    20        INIT_FCALL                                                   'run_tests'
         21        SEND_VAR                                                     !3
         22        SEND_VAL                                                     <true>
         23        DO_FCALL                                          0          
         24      > RETURN                                                       1

Function sanitize_hex_color:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
2 jumps found. (Code = 46) Position 1 = 15, Position 2 = 19
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 21
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
Branch analysis from position: 8
2 jumps found. (Code = 46) Position 1 = 15, Position 2 = 19
Branch analysis from position: 15
Branch analysis from position: 19
filename:       /in/4s5Q6
function name:  sanitize_hex_color
number of ops:  22
compiled vars:  !0 = $color, !1 = $maybe_alpha, !2 = $allowed_lengths, !3 = $correct_length
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      <false>
    4     2        IS_IDENTICAL                                                 !0, ''
          3      > JMPZ                                                         ~4, ->5
    5     4    > > RETURN                                                       ''
    8     5    > > JMPZ                                                         !1, ->8
          6    >   QM_ASSIGN                                            ~5      <array>
          7      > JMP                                                          ->9
          8    >   QM_ASSIGN                                            ~5      <array>
          9    >   ASSIGN                                                       !2, ~5
    9    10        STRLEN                                               ~7      !0
         11        FRAMELESS_ICALL_3                in_array            ~8      ~7, !2
         12        OP_DATA                                                      <true>
         13        ASSIGN                                                       !3, ~8
   11    14      > JMPZ_EX                                              ~10     !3, ->19
         15    >   FRAMELESS_ICALL_3                preg_replace        ~11     '%7C%5E%23%28%5B%5EA-Fa-f0-9%5D%2B%29%24%7C', ''
         16        OP_DATA                                                      !0
         17        IS_IDENTICAL                                         ~12     !0, ~11
         18        BOOL                                                 ~10     ~12
         19    > > JMPZ                                                         ~10, ->21
   12    20    > > RETURN                                                       !0
   14    21    > > RETURN                                                       null

End of function sanitize_hex_color

Function run_tests:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 48
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 48
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 39
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 44
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 44
Branch analysis from position: 42
Branch analysis from position: 44
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
Branch analysis from position: 10
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 48
Branch analysis from position: 14
Branch analysis from position: 48
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
Branch analysis from position: 10
filename:       /in/4s5Q6
function name:  run_tests
number of ops:  50
compiled vars:  !0 = $tests, !1 = $testdox, !2 = $markers, !3 = $data, !4 = $test, !5 = $color, !6 = $expected, !7 = $maybe_alpha, !8 = $actual
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
  222     0  E >   RECV_INIT                                            !0      <array>
          1        RECV_INIT                                            !1      <false>
  224     2      > JMPZ                                                         !1, ->5
          3    >   QM_ASSIGN                                            ~9      '%E2%9D%8C'
          4      > JMP                                                          ->6
          5    >   QM_ASSIGN                                            ~9      'F'
          6    >   INIT_ARRAY                                           ~10     ~9
  225     7      > JMPZ                                                         !1, ->10
          8    >   QM_ASSIGN                                            ~11     '%E2%9C%85'
          9      > JMP                                                          ->11
         10    >   QM_ASSIGN                                            ~11     '.'
         11    >   ADD_ARRAY_ELEMENT                                    ~10     ~11
  223    12        ASSIGN                                                       !2, ~10
  228    13      > FE_RESET_R                                           $13     !0, ->48
         14    > > FE_FETCH_R                                           ~14     $13, !3, ->48
         15    >   ASSIGN                                                       !4, ~14
  229    16        INIT_FCALL                                                   'array_values'
         17        SEND_VAR                                                     !3
         18        DO_ICALL                                             $16     
         19        FETCH_LIST_R                                         $17     $16, 0
         20        ASSIGN                                                       !5, $17
         21        FETCH_LIST_R                                         $19     $16, 1
         22        ASSIGN                                                       !6, $19
         23        FETCH_LIST_R                                         $21     $16, 2
         24        ASSIGN                                                       !7, $21
         25        FREE                                                         $16
  231    26        INIT_FCALL                                                   'sanitize_hex_color'
         27        SEND_VAR                                                     !5
         28        SEND_VAR                                                     !7
         29        DO_FCALL                                          0  $23     
         30        ASSIGN                                                       !8, $23
  233    31        IS_IDENTICAL                                         ~25     !8, !6
         32        CAST                                              4  ~26     ~25
         33        FETCH_DIM_R                                          ~27     !2, ~26
         34      > JMPZ                                                         !1, ->39
         35    >   NOP                                                          
         36        FAST_CONCAT                                          ~28     '+', !4
         37        QM_ASSIGN                                            ~29     ~28
         38      > JMP                                                          ->40
         39    >   QM_ASSIGN                                            ~29     ''
         40    >   CONCAT                                               ~30     ~27, ~29
         41      > JMPZ                                                         !1, ->44
         42    >   QM_ASSIGN                                            ~31     '%0A'
         43      > JMP                                                          ->45
         44    >   QM_ASSIGN                                            ~31     ''
         45    >   CONCAT                                               ~32     ~30, ~31
         46        ECHO                                                         ~32
  228    47      > JMP                                                          ->14
         48    >   FE_FREE                                                      $13
  235    49      > RETURN                                                       null

End of function run_tests

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
212.6 ms | 2135 KiB | 19 Q