3v4l.org

run code in 300+ 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)"));
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.4.16, 8.5.0 - 8.5.1
<pre>Normal value: Array ( [r] => 66 [g] => 66 [b] => 66 [a] => 255 ) Red > 255: Array ( [r] => 255 [g] => 66 [b] => 66 [a] => 255 ) Green < 0: Array ( [r] => 66 [g] => 0 [b] => 66 [a] => 255 ) Alpha float (normal): Array ( [r] => 66 [g] => 66 [b] => 66 [a] => 153 ) Alpha overflow: Array ( [r] => 66 [g] => 66 [b] => 66 [a] => 255 )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
193.7 ms | 407 KiB | 5 Q