3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Wrapper for strpbrk() except that it accepts a $length parameter * which will restrict the result string to this many characters. * * @param string $haystack The string where char_list is looked for. * @param string $char_list Case sensitive set of characters to look for. * @param int $length [optional] If given and is positive, the string returned will contain at most length characters. * @return false|string * @throws RangeException */ function strpbrkLength( string $haystack, string $char_list, int $length = null ) /*: false|string*/ { if( null !== $length && 0 > $length ) throw new RangeException( '$length must be null, zero or a positive integer.' ); $strpbrk = strpbrk( $haystack, $char_list ); if( false !== $strpbrk && null !== $length && strlen( $strpbrk ) > $length ) return substr( $strpbrk, 0, $length ); return $strpbrk; } $tests = [ [ 'haystack' => '', 'char_list' => 'r+', 'length' => null, 'expect' => false ], [ 'haystack' => '', 'char_list' => 'r+', 'length' => 0 , 'expect' => false ], [ 'haystack' => '', 'char_list' => 'r+', 'length' => 1 , 'expect' => false ], [ 'haystack' => 'r', 'char_list' => 'r+', 'length' => null, 'expect' => 'r' ], [ 'haystack' => 'r', 'char_list' => 'r+', 'length' => 0 , 'expect' => '' ], [ 'haystack' => 'r', 'char_list' => 'r+', 'length' => 1 , 'expect' => 'r' ], [ 'haystack' => 'r+', 'char_list' => 'r+', 'length' => null, 'expect' => 'r+' ], [ 'haystack' => 'r+', 'char_list' => 'r+', 'length' => 0 , 'expect' => '' ], [ 'haystack' => 'r+', 'char_list' => 'r+', 'length' => 1 , 'expect' => 'r' ], [ 'haystack' => 'r+', 'char_list' => 'r+', 'length' => 2 , 'expect' => 'r+' ], [ 'haystack' => 'w', 'char_list' => 'r+', 'length' => null, 'expect' => false ], [ 'haystack' => 'w', 'char_list' => 'r+', 'length' => 0, 'expect' => false ], [ 'haystack' => 'w', 'char_list' => 'r+', 'length' => 1, 'expect' => false ], [ 'haystack' => 'w+', 'char_list' => 'r+', 'length' => null, 'expect' => '+' ], [ 'haystack' => 'w+', 'char_list' => 'r+', 'length' => 0 , 'expect' => '' ], [ 'haystack' => 'w+', 'char_list' => 'r+', 'length' => 1 , 'expect' => '+' ], ]; foreach( $tests as $test => $args ) assert( strpbrkLength( $args[ 'haystack' ], $args[ 'char_list' ], $args[ 'length' ] ) === $args[ 'expect' ], "Failed test #$test" );

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.0.20.0120.01016.92
8.0.00.0120.00616.79
7.4.130.0100.00716.59
7.4.120.0060.01316.53
7.4.110.0100.00616.42
7.4.100.0130.00616.38
7.4.90.0120.00916.58
7.4.80.0070.01119.39
7.4.70.0100.00716.71
7.4.60.0090.00616.64
7.4.50.0070.00716.54
7.4.40.0030.01316.29
7.4.00.0070.01015.14
7.3.260.0090.00916.46
7.3.240.0110.00716.41
7.3.230.0030.01316.34
7.3.210.0030.01416.30
7.3.200.0150.00216.31
7.3.190.0110.01316.32
7.3.180.0070.01016.51
7.3.170.0100.01016.58
7.3.160.0130.00316.58
7.3.110.0100.01014.73
7.3.100.0030.01014.66
7.3.90.0040.01114.99
7.3.80.0060.00914.63
7.3.70.0130.00314.85
7.3.60.0030.01014.75
7.3.50.0050.00814.66
7.3.40.0050.01314.77
7.3.30.0080.00615.00
7.3.20.0070.00714.77
7.3.10.0090.00614.85
7.3.00.0030.01114.71
7.2.330.0080.01116.78
7.2.320.0040.01216.71
7.2.310.0100.00716.50
7.2.300.0130.00316.46
7.2.290.0090.01216.63
7.2.240.0100.00515.21
7.2.230.0050.00915.04
7.2.220.0080.00615.07
7.2.210.0060.01015.04
7.2.200.0080.00615.25
7.2.190.0030.01115.12
7.2.180.0110.00714.98
7.2.170.0070.00715.02
7.2.160.0040.01115.10
7.2.150.0060.00915.13
7.2.140.0150.00414.98
7.2.130.0060.01515.21
7.2.120.0140.00014.91
7.2.110.0080.00815.23
7.2.100.0200.00015.27
7.2.90.0100.01015.04
7.2.80.0090.00615.09
7.2.70.0040.01115.29
7.2.60.0120.00415.20
7.2.50.0080.00815.16
7.2.40.0060.00915.18
7.2.30.0060.00915.12
7.2.20.0090.00615.13
7.2.10.0110.00515.05
7.2.00.0040.01115.09
7.1.330.0090.00613.91
7.1.320.0060.00614.15
7.1.310.0250.00914.09
7.1.300.0030.01113.97
7.1.290.0060.01314.15
7.1.280.0030.01013.94
7.1.270.0070.00713.92
7.1.260.0030.01013.85
7.1.250.0030.01113.66