3v4l.org

run code in 500+ PHP versions simultaneously
<?php /** * Parses an RGBA string and returns and array with each color * @param string $str The RGBA string, ex: "rgba(66, 66, 66, 1)" * @return array Returns an array with each r, g, b and a value or false if an invalid string is passed in. */ function parseRGBA($str){ //match the rgba string and get it's part if(preg_match('/rgba\( *([\d\.-]+), *([\d\.-]+), *([\d\.-]+), *([\d\.-]+) *\)/i', $str, $m)){ $out = array( 'r'=>intval($m[1]), //get the red 'g'=>intval($m[2]), //get the green 'b'=>intval($m[3]), //get the blue 'a'=>round(floatval($m[4]) * 255), //get the alpha and scale to 0-255 ); //clamp each value between 0 and 255 array_walk($out, function(&$v){ $v = min(255, max(0, $v)); }); return $out; } return false; } echo '<pre>'; echo "Normal value: "; print_r(parseRGBA("rgba(66, 66, 66, 1)")); echo "Red > 255: "; print_r(parseRGBA("rgba(660, 66, 66, 1)")); echo "Green < 0: "; print_r(parseRGBA("rgba(66, -66, 66, 1)")); echo "Alpha float (normal): "; print_r(parseRGBA("rgba(66, 66, 66, 0.6)")); echo "Alpha overflow: "; print_r(parseRGBA("rgba(66, 66, 66, 10)"));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1iUh1
function name:  (null)
number of ops:  37
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   25     0  E >   ECHO                                                         '%3Cpre%3E'
   26     1        ECHO                                                         'Normal+value%3A+'
   27     2        INIT_FCALL                                                   'print_r'
          3        INIT_FCALL                                                   'parsergba'
          4        SEND_VAL                                                     'rgba%2866%2C+66%2C+66%2C+1%29'
          5        DO_FCALL                                          0  $0      
          6        SEND_VAR                                                     $0
          7        DO_ICALL                                                     
   29     8        ECHO                                                         'Red+%3E+255%3A+'
   30     9        INIT_FCALL                                                   'print_r'
         10        INIT_FCALL                                                   'parsergba'
         11        SEND_VAL                                                     'rgba%28660%2C+66%2C+66%2C+1%29'
         12        DO_FCALL                                          0  $2      
         13        SEND_VAR                                                     $2
         14        DO_ICALL                                                     
   32    15        ECHO                                                         'Green+%3C+0%3A+'
   33    16        INIT_FCALL                                                   'print_r'
         17        INIT_FCALL                                                   'parsergba'
         18        SEND_VAL                                                     'rgba%2866%2C+-66%2C+66%2C+1%29'
         19        DO_FCALL                                          0  $4      
         20        SEND_VAR                                                     $4
         21        DO_ICALL                                                     
   35    22        ECHO                                                         'Alpha+float+%28normal%29%3A+'
   36    23        INIT_FCALL                                                   'print_r'
         24        INIT_FCALL                                                   'parsergba'
         25        SEND_VAL                                                     'rgba%2866%2C+66%2C+66%2C+0.6%29'
         26        DO_FCALL                                          0  $6      
         27        SEND_VAR                                                     $6
         28        DO_ICALL                                                     
   38    29        ECHO                                                         'Alpha+overflow%3A+'
   39    30        INIT_FCALL                                                   'print_r'
         31        INIT_FCALL                                                   'parsergba'
         32        SEND_VAL                                                     'rgba%2866%2C+66%2C+66%2C+10%29'
         33        DO_FCALL                                          0  $8      
         34        SEND_VAR                                                     $8
         35        DO_ICALL                                                     
         36      > RETURN                                                       1

Function parsergba:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 30
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1iUh1
function name:  parseRGBA
number of ops:  32
compiled vars:  !0 = $str, !1 = $m, !2 = $out
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    8     0  E >   RECV                                                 !0      
   10     1        INIT_FCALL                                                   'preg_match'
          2        SEND_VAL                                                     '%2Frgba%5C%28+%2A%28%5B%5Cd%5C.-%5D%2B%29%2C+%2A%28%5B%5Cd%5C.-%5D%2B%29%2C+%2A%28%5B%5Cd%5C.-%5D%2B%29%2C+%2A%28%5B%5Cd%5C.-%5D%2B%29+%2A%5C%29%2Fi'
          3        SEND_VAR                                                     !0
          4        SEND_REF                                                     !1
          5        DO_ICALL                                             $3      
          6      > JMPZ                                                         $3, ->30
   12     7    >   FETCH_DIM_R                                          ~4      !1, 1
          8        CAST                                              4  ~5      ~4
          9        INIT_ARRAY                                           ~6      ~5, 'r'
   13    10        FETCH_DIM_R                                          ~7      !1, 2
         11        CAST                                              4  ~8      ~7
         12        ADD_ARRAY_ELEMENT                                    ~6      ~8, 'g'
   14    13        FETCH_DIM_R                                          ~9      !1, 3
         14        CAST                                              4  ~10     ~9
         15        ADD_ARRAY_ELEMENT                                    ~6      ~10, 'b'
   15    16        INIT_FCALL                                                   'round'
         17        FETCH_DIM_R                                          ~11     !1, 4
         18        CAST                                              5  ~12     ~11
         19        MUL                                                  ~13     ~12, 255
         20        SEND_VAL                                                     ~13
         21        DO_ICALL                                             $14     
         22        ADD_ARRAY_ELEMENT                                    ~6      $14, 'a'
   11    23        ASSIGN                                                       !2, ~6
   19    24        INIT_FCALL                                                   'array_walk'
         25        SEND_REF                                                     !2
         26        DECLARE_LAMBDA_FUNCTION                              ~16     [0]
         27        SEND_VAL                                                     ~16
         28        DO_ICALL                                                     
   21    29      > RETURN                                                       !2
   23    30    > > RETURN                                                       <false>
   24    31*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1iUh1
function name:  {closure:parseRGBA():19}
number of ops:  5
compiled vars:  !0 = $v
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   19     0  E >   RECV                                                 !0      
          1        FRAMELESS_ICALL_2                max                 ~1      0, !0
          2        FRAMELESS_ICALL_2                min                 ~2      255, ~1
          3        ASSIGN                                                       !0, ~2
          4      > RETURN                                                       null

End of Dynamic Function 0

End of function parsergba

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
171.31 ms | 2681 KiB | 22 Q