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 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 );

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.3.60.0120.00618.43
8.3.50.0040.01420.37
8.3.40.0030.01218.96
8.3.30.0040.01119.21
8.3.20.0080.00024.18
8.3.10.0000.00824.66
8.3.00.0050.00526.16
8.2.180.0150.00025.92
8.2.170.0170.00319.17
8.2.160.0030.01222.96
8.2.150.0080.00025.66
8.2.140.0080.00024.66
8.2.130.0070.01126.16
8.2.120.0060.00326.16
8.2.110.0120.01222.25
8.2.100.0040.00818.15
8.2.90.0090.00019.40
8.2.80.0040.00718.04
8.2.70.0040.00417.88
8.2.60.0040.00417.93
8.2.50.0040.00418.07
8.2.40.0000.00818.32
8.2.30.0050.00318.13
8.2.20.0040.00420.40
8.2.10.0000.00718.13
8.2.00.0040.00419.30
8.1.280.0060.01625.92
8.1.270.0040.00423.99
8.1.260.0040.00426.35
8.1.250.0050.00328.09
8.1.240.0050.00520.89
8.1.230.0080.00417.77
8.1.220.0090.00017.79
8.1.210.0040.00418.77
8.1.200.0060.00617.48
8.1.190.0000.00817.47
8.1.180.0040.00418.10
8.1.170.0040.00418.87
8.1.160.0040.00418.93
8.1.150.0040.00418.88
8.1.140.0040.00417.74
8.1.130.0030.00319.01
8.1.120.0000.00717.55
8.1.110.0030.00617.68
8.1.100.0040.00417.63
8.1.90.0040.00417.59
8.1.80.0000.00817.69
8.1.70.0180.00017.61
8.1.60.0200.00317.68
8.1.50.0120.00817.70
8.1.40.0170.00017.76
8.1.30.0120.00817.68
8.1.20.0120.00817.81
8.1.10.0100.01017.66
8.1.00.0100.01017.72
8.0.300.0030.00518.77
8.0.290.0030.00616.88
8.0.280.0000.00718.49
8.0.270.0000.00718.13
8.0.260.0030.00318.50
8.0.250.0000.00717.04
8.0.240.0030.00317.16
8.0.230.0000.00917.06
8.0.220.0030.00317.05
8.0.210.0030.00317.00
8.0.200.0050.01017.10
8.0.190.0180.00017.07
8.0.180.0120.00517.09
8.0.170.0150.00216.95
8.0.160.0140.00517.07
8.0.150.0170.00016.89
8.0.140.0130.00317.05
8.0.130.0150.00317.10
8.0.120.0070.01117.05
8.0.110.0140.00317.06
8.0.100.0080.00816.99
8.0.90.0140.00317.04
8.0.80.0120.00417.11
8.0.70.0170.00016.95
8.0.60.0110.00517.05
8.0.50.0120.00416.90
8.0.30.0120.00616.88
8.0.20.0070.00717.03
8.0.10.0160.00017.17
7.4.330.0000.00615.55
7.4.320.0040.00416.76
7.4.300.0140.00516.76
7.4.290.0050.01116.73
7.4.280.0180.00016.86
7.4.270.0100.01016.68
7.4.260.0140.00416.80
7.4.250.0070.01116.63
7.4.240.0120.00316.88
7.4.230.0090.00916.81
7.4.220.0130.00416.72
7.4.210.0130.00316.82
7.4.200.0180.00016.85
7.4.190.0100.00616.91
7.4.180.0110.00616.91
7.4.160.0150.00416.80
7.4.150.0190.00016.69
7.4.140.0080.00816.61
7.4.130.0100.00516.79
7.4.120.0140.00316.71
7.4.110.0150.00316.76
7.4.100.0040.01116.86
7.4.90.0110.00516.61
7.4.80.0120.00516.67
7.4.70.0070.00316.66
7.4.60.0130.00016.73
7.4.50.0120.00216.57
7.4.40.0120.00216.57
7.4.30.0080.00916.81
7.4.20.0110.00016.68
7.4.10.0120.00016.67
7.4.00.0050.00516.54

preferences:
52.28 ms | 400 KiB | 5 Q