3v4l.org

run code in 300+ 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 1 alpha' => array( 'color' => '#abcff', 'expected' => null, 'maybe_alpha' => true, ), '$maybe_alpha = true, 6 mixed with 1 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\n" : '' ); } } 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 );

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.5.00.0120.01020.32
8.4.150.0020.00014.05
8.4.140.0120.00917.58
8.4.130.0100.00817.89
8.4.120.0100.00620.66
8.4.110.0120.00822.25
8.4.100.0040.00618.02
8.4.90.0120.00418.95
8.4.80.0040.00419.81
8.4.70.0140.00718.00
8.4.60.0050.00320.61
8.4.50.0050.00618.06
8.4.40.0160.00319.53
8.4.30.0130.00722.19
8.4.20.0170.00320.57
8.4.10.0040.00423.95
8.3.280.0110.00818.70
8.3.270.0130.00716.87
8.3.260.0120.00816.98
8.3.250.0120.00719.04
8.3.240.0160.00416.62
8.3.230.0130.00616.73
8.3.220.0050.00317.42
8.3.210.0100.00916.99
8.3.200.0040.00416.85
8.3.190.0060.01419.02
8.3.180.0090.01116.59
8.3.170.0030.01518.78
8.3.160.0130.00718.50
8.3.150.0140.00418.94
8.3.140.0090.00916.60
8.3.130.0000.00918.56
8.3.120.0000.00920.67
8.3.110.0080.00020.94
8.3.100.0060.00316.68
8.3.90.0040.00426.77
8.3.80.0000.01018.31
8.3.70.0120.00918.55
8.3.60.0140.00418.17
8.3.50.0030.01220.49
8.3.40.0160.00618.95
8.3.30.0100.00719.15
8.3.20.0070.00024.18
8.3.10.0030.00624.66
8.3.00.0070.00426.16
8.2.290.0140.00520.43
8.2.280.0050.00418.54
8.2.270.0100.01017.53
8.2.260.0070.01118.77
8.2.250.0040.00417.18
8.2.240.0080.00419.28
8.2.230.0110.00022.58
8.2.220.0080.00024.06
8.2.210.0150.00326.77
8.2.200.0090.00018.41
8.2.190.0150.00716.63
8.2.180.0150.00025.92
8.2.170.0070.01122.96
8.2.160.0080.00822.96
8.2.150.0040.00425.66
8.2.140.0000.00824.66
8.2.130.0040.00426.16
8.2.120.0040.00421.17
8.2.110.0060.00321.19
8.2.100.0140.00318.03
8.2.90.0040.00418.13
8.2.80.0000.00918.00
8.2.70.0030.00617.88
8.2.60.0040.00417.88
8.2.50.0000.00817.98
8.2.40.0030.00518.28
8.2.30.0000.00818.13
8.2.20.0040.00420.50
8.2.10.0040.00418.24
8.2.00.0030.00619.40
8.1.330.0110.00922.31
8.1.320.0100.00716.56
8.1.310.0150.00418.56
8.1.300.0000.00818.21
8.1.290.0100.00030.84
8.1.280.0160.00625.92
8.1.270.0040.00423.99
8.1.260.0030.01226.35
8.1.250.0000.00728.09
8.1.240.0170.00323.84
8.1.230.0060.00617.83
8.1.220.0050.00317.88
8.1.210.0060.00318.77
8.1.200.0080.00017.60
8.1.190.0000.00817.38
8.1.180.0000.00918.10
8.1.170.0030.00617.62
8.1.160.0040.00418.89
8.1.150.0070.00018.77
8.1.140.0000.00917.56
8.1.130.0030.00318.91
8.1.120.0000.00817.66
8.1.110.0040.00417.62
8.1.100.0040.00417.66
8.1.90.0050.00317.68
8.1.80.0040.00417.55
8.1.70.0130.00417.68
8.1.60.0080.00817.79
8.1.50.0100.00717.71
8.1.40.0150.00417.60
8.1.30.0190.00117.83
8.1.20.0170.00417.80
8.1.10.0140.00517.74
8.1.00.0190.00117.69
8.0.300.0020.00518.77
8.0.290.0040.00417.00
8.0.280.0040.00418.51
8.0.270.0070.00016.93
8.0.260.0030.00318.48
8.0.250.0000.00717.19
8.0.240.0030.00317.03
8.0.230.0050.00317.10
8.0.220.0000.00717.09
8.0.210.0000.00716.95
8.0.200.0100.00617.10
8.0.190.0160.00117.03
8.0.180.0130.00517.07
8.0.170.0130.00417.04
8.0.160.0090.00317.11
8.0.150.0070.00617.04
8.0.140.0090.00517.08
8.0.130.0080.00717.14
8.0.120.0090.00517.12
8.0.110.0080.00817.03
8.0.100.0100.00817.12
8.0.90.0090.00717.08
8.0.80.0140.00317.04
8.0.70.0150.00316.97
8.0.60.0120.00617.00
8.0.50.0080.00816.94
8.0.30.0140.00216.98
8.0.20.0110.00317.06
8.0.10.0110.00317.21
8.0.00.0090.00717.23
7.4.330.0030.00315.55
7.4.320.0050.00316.77
7.4.300.0130.00416.74
7.4.290.0140.00316.88
7.4.280.0100.00516.77
7.4.270.0120.00416.81
7.4.260.0110.00516.75
7.4.250.0110.00416.70
7.4.240.0120.00216.88
7.4.230.0120.00516.84
7.4.220.0110.00416.76
7.4.210.0070.00716.80
7.4.200.0070.00616.67
7.4.190.0080.00616.90
7.4.180.0100.00416.85
7.4.160.0130.00216.74
7.4.150.0090.00516.82
7.4.140.0070.00716.58
7.4.130.0090.00516.62
7.4.120.0130.00216.65
7.4.110.0130.00316.68
7.4.100.0120.00716.86
7.4.90.0100.00816.82
7.4.80.0110.00516.69
7.4.70.0110.00316.63
7.4.60.0130.00116.76
7.4.50.0120.00416.72
7.4.40.0090.00716.70
7.4.30.0130.00316.79
7.4.20.0110.00416.58
7.4.10.0060.00816.71
7.4.00.0070.00716.68
7.3.330.0080.00816.63
7.3.320.0110.00616.56
7.3.310.0130.00316.65
7.3.300.0110.00616.71
7.3.290.0100.00516.46
7.3.280.0090.00916.58
7.3.270.0100.00616.38
7.3.260.0100.00516.42
7.3.250.0180.00016.46
7.3.240.0100.00716.60
7.3.230.0150.00316.57
7.3.220.0130.00316.37
7.3.210.0140.00216.36
7.3.200.0110.00616.48
7.3.190.0170.00016.53
7.3.180.0130.00316.58
7.3.170.0160.00016.53
7.3.160.0140.00516.53
7.3.150.0100.00616.61
7.3.140.0140.00416.39
7.3.130.0140.00016.36
7.3.120.0090.00516.54
7.3.110.0090.00616.48
7.3.100.0070.01016.47
7.3.90.0140.00316.86
7.3.80.0120.00316.56
7.3.70.0170.00016.64
7.3.60.0160.00016.48
7.3.50.0120.00416.64
7.3.40.0060.01216.57
7.3.30.0120.00316.64
7.3.20.0100.00618.40
7.3.10.0080.00818.39
7.3.00.0170.00018.43
7.2.340.0110.00516.75
7.2.330.0120.00516.73
7.2.320.0150.00016.61
7.2.310.0100.00716.61
7.2.300.0090.00916.82
7.2.290.0120.00616.75
7.2.280.0110.00516.55
7.2.270.0080.00716.85
7.2.260.0120.00416.80
7.2.250.0140.00316.80
7.2.240.0100.00616.87
7.2.230.0120.00316.77
7.2.220.0130.00416.77
7.2.210.0110.00516.59
7.2.200.0130.00416.84
7.2.190.0060.01016.70
7.2.180.0100.00516.68
7.2.170.0150.00016.89
7.2.160.0080.00816.76
7.2.150.0120.00518.58
7.2.140.0160.00018.78
7.2.130.0080.00818.88
7.2.120.0100.00518.63
7.2.110.0080.00818.62
7.2.100.0120.00518.69
7.2.90.0070.01018.65
7.2.80.0160.00018.78
7.2.70.0160.00018.71
7.2.60.0070.01018.85
7.2.50.0110.00418.68
7.2.40.0110.00618.70
7.2.30.0090.00718.59
7.2.20.0000.01618.66
7.2.10.0110.00518.66
7.2.00.0100.00718.76
7.1.330.0140.00017.49
7.1.320.0040.01017.34
7.1.310.0150.00017.39
7.1.300.0110.00417.51
7.1.290.0130.00317.38
7.1.280.0150.00017.32
7.1.270.0110.00417.61
7.1.260.0160.00017.45
7.1.250.0120.00417.41
7.1.240.0120.00417.44
7.1.230.0120.00417.30
7.1.220.0100.00517.46
7.1.210.0110.00317.55
7.1.200.0100.00517.28
7.1.190.0140.00017.25
7.1.180.0140.00017.36
7.1.170.0150.00017.43
7.1.160.0110.00417.30
7.1.150.0100.00717.25
7.1.140.0080.00817.53
7.1.130.0240.00317.25
7.1.120.0130.00417.24
7.1.110.0120.00317.52
7.1.100.0080.00817.63
7.1.90.0140.00017.46
7.1.80.0140.00017.57
7.1.70.0160.00017.46
7.1.60.0050.01017.54
7.1.50.0090.00517.62
7.1.40.0000.01617.51
7.1.30.0150.00017.61
7.1.20.0160.00017.51
7.1.10.0070.00717.51
7.1.00.0100.00517.48
7.0.330.0090.00517.33
7.0.320.0100.00517.08
7.0.310.0100.00516.94
7.0.300.0110.00417.08
7.0.290.0150.00017.23
7.0.280.0140.00017.24
7.0.270.0120.00417.26
7.0.260.0110.00417.08
7.0.250.0160.00017.22
7.0.240.0080.00517.13
7.0.230.0100.00317.31
7.0.220.0120.00317.30
7.0.210.0080.00617.24
7.0.200.0110.00417.26
7.0.190.0110.00417.33
7.0.180.0100.00317.18
7.0.170.0090.00517.25
7.0.160.0080.00817.22
7.0.150.0150.00017.29
7.0.140.0090.00617.11
7.0.130.0090.00617.11
7.0.120.0110.00317.28
7.0.110.0050.00917.20
7.0.100.0110.00317.10
7.0.90.0090.00617.19
7.0.80.0130.00217.17
7.0.70.0110.00417.21
7.0.60.0000.01417.04
7.0.50.0090.00416.95
7.0.40.0110.00417.00
7.0.30.0100.00317.00
7.0.20.0050.00816.93
7.0.10.0090.00517.06
7.0.00.0140.00017.13
5.6.400.0060.00916.02
5.6.390.0110.00316.09
5.6.380.0120.00215.86
5.6.370.0100.00516.13
5.6.360.0090.00616.04
5.6.350.0100.00515.99
5.6.340.0070.00716.03
5.6.330.0120.00215.82
5.6.320.0150.00016.10
5.6.310.0070.00716.01
5.6.300.0070.00715.88
5.6.290.0160.00016.02
5.6.280.0140.00016.05
5.6.270.0140.00015.86
5.6.260.0110.00316.01
5.6.250.0040.01216.00
5.6.240.0090.00516.05
5.6.230.0080.00616.12
5.6.220.0150.00016.24
5.6.210.0120.00316.02
5.6.200.0110.00416.23
5.6.190.0080.00816.23
5.6.180.0000.01416.07
5.6.170.0140.00016.34
5.6.160.0120.00316.18
5.6.150.0050.01116.29
5.6.140.0150.00016.30
5.6.130.0100.00516.30
5.6.120.0150.00016.32
5.6.110.0080.00815.96
5.6.100.0090.00615.99
5.6.90.0120.00316.05
5.6.80.0050.01015.96
5.6.70.0140.00016.17
5.6.60.0140.00016.20
5.6.50.0090.00615.99
5.6.40.0150.00016.01
5.6.30.0100.00316.10
5.6.20.0100.00316.08
5.6.10.0110.00416.10
5.6.00.0130.00016.02
5.5.380.0050.01016.08
5.5.370.0120.00216.16
5.5.360.0130.00316.06
5.5.350.0080.00615.73
5.5.340.0130.00015.77
5.5.330.0100.00515.98
5.5.320.0110.00315.98
5.5.310.0150.00015.78
5.5.300.0040.01115.81
5.5.290.0130.00015.91
5.5.280.0090.00415.96
5.5.270.0110.00316.05
5.5.260.0030.01015.76
5.5.250.0090.00616.19
5.5.240.0000.01415.90
5.5.230.0070.00715.92
5.5.220.0110.00416.05
5.5.210.0070.00715.98
5.5.200.0130.00316.11
5.5.190.0110.00316.07
5.5.180.0130.00015.88
5.5.170.0100.00315.81
5.5.160.0090.00415.90
5.5.150.0140.00015.92
5.5.140.0150.00015.86
5.5.130.0090.00615.86
5.5.120.0090.00415.77
5.5.110.0090.00515.95
5.5.100.0140.00016.07
5.5.90.0080.00516.04
5.5.80.0100.00515.88
5.5.70.0070.00716.08
5.5.60.0070.00715.97
5.5.50.0130.00015.86
5.5.40.0070.00516.06
5.5.30.0050.00816.17
5.5.20.0000.01516.06
5.5.10.0100.00515.76
5.5.00.0000.01416.09
5.4.450.0080.00312.87
5.4.440.0110.00012.89
5.4.430.0100.00012.89
5.4.420.0070.00312.92
5.4.410.0100.00312.88
5.4.400.0060.00612.96
5.4.390.0120.00012.88
5.4.380.0100.00312.79
5.4.370.0110.00012.88
5.4.360.0040.00712.55
5.4.350.0110.00012.96
5.4.340.0080.00412.59
5.4.330.0100.00012.68
5.4.320.0130.00012.71
5.4.310.0080.00312.87
5.4.300.0060.00512.93
5.4.290.0060.00612.84
5.4.280.0070.00412.72
5.4.270.0120.00012.66
5.4.260.0060.00712.70
5.4.250.0120.00012.96
5.4.240.0070.00512.61
5.4.230.0090.00312.46
5.4.220.0080.00412.62
5.4.210.0090.00312.44
5.4.200.0100.00212.58
5.4.190.0090.00312.73
5.4.180.0080.00412.41
5.4.170.0110.00012.76
5.4.160.0050.00512.71
5.4.150.0040.00712.59
5.4.140.0070.00412.81
5.4.130.0070.00212.69
5.4.120.0060.00612.87
5.4.110.0080.00413.02
5.4.100.0110.00012.70
5.4.90.0120.00012.93
5.4.80.0080.00412.92
5.4.70.0050.00512.91
5.4.60.0050.00513.00
5.4.50.0110.00012.79
5.4.40.0050.00612.71
5.4.30.0100.00012.93
5.4.20.0050.00512.78
5.4.10.0040.00812.98
5.4.00.0080.00312.62
5.3.290.0110.00013.08
5.3.280.0050.00513.16
5.3.270.0070.00513.06
5.3.260.0140.00013.29
5.3.250.0070.00413.18
5.3.240.0070.00513.14
5.3.230.0000.01212.96
5.3.220.0000.01012.99
5.3.210.0080.00412.92
5.3.200.0000.01013.19
5.3.190.0100.00313.01
5.3.180.0080.00313.34
5.3.170.0040.00813.14
5.3.160.0090.00313.13
5.3.150.0090.00213.10
5.3.140.0100.00313.17
5.3.130.0040.00713.10
5.3.120.0070.00413.31
5.3.110.0060.00613.09
5.3.100.0080.00313.04
5.3.90.0070.00413.10
5.3.80.0070.00313.11
5.3.70.0050.00513.13
5.3.60.0100.00013.32
5.3.50.0000.01112.93
5.3.40.0050.00512.93
5.3.30.0070.00313.07
5.3.20.0080.00313.05
5.3.10.0060.00612.83
5.3.00.0090.00312.80
5.2.170.0100.00012.18
5.2.160.0050.00511.88
5.2.150.0060.00312.21
5.2.140.0040.00712.10
5.2.130.0090.00011.85
5.2.120.0050.00511.99
5.2.110.0070.00211.94
5.2.100.0050.00512.16
5.2.90.0050.00511.89
5.2.80.0040.00611.78
5.2.70.0070.00212.07
5.2.60.0100.00011.90
5.2.50.0050.00512.01
5.2.40.0030.00612.05
5.2.30.0030.00812.00
5.2.20.0030.00611.66
5.2.10.0040.00411.86
5.2.00.0040.00511.80
5.1.60.0070.00011.14
5.1.50.0070.00010.92
5.1.40.0040.00311.00
5.1.30.0030.00611.35
5.1.20.0080.00011.45
5.1.10.0000.00711.32
5.1.00.0090.00010.89
5.0.50.0070.00010.45
5.0.40.0030.00310.45
5.0.30.0030.00310.45
5.0.20.0070.00010.45
5.0.10.0000.00610.45
5.0.00.0040.00210.45
4.4.90.0040.00010.45
4.4.80.0000.00510.45
4.4.70.0000.00510.45
4.4.60.0030.00210.45
4.4.50.0000.00410.45
4.4.40.0030.00210.45
4.4.30.0000.00410.45
4.4.20.0020.00210.45
4.4.10.0030.00310.45
4.4.00.0050.00010.45
4.3.110.0000.00410.45
4.3.100.0040.00010.45
4.3.90.0000.00510.45
4.3.80.0020.00210.45
4.3.70.0000.00510.45
4.3.60.0050.00010.45
4.3.50.0040.00010.45
4.3.40.0040.00010.45
4.3.30.0030.00010.45
4.3.20.0030.00010.45
4.3.10.0020.00210.45
4.3.00.0040.00010.45

preferences:
150.64 ms | 403 KiB | 5 Q