3v4l.org

run code in 300+ PHP versions simultaneously
<?php function truncateGumbo($string, $chars = 50, $terminator = ' …') { $cutPos = $chars - mb_strlen($terminator); $boundaryPos = mb_strrpos(mb_substr($string, 0, mb_strpos($string, ' ', $cutPos)), ' '); return mb_substr($string, 0, $boundaryPos === false ? $cutPos : $boundaryPos) . $terminator; } function truncateGordon($string, $chars = 50, $terminator = ' …') { return mb_strimwidth($string, 0, $chars, $terminator); } function truncateSoapBox($string, $chars = 50, $terminate = ' …') { $chars -= mb_strlen($terminate); if ( $chars <= 0 ) return $terminate; $string = mb_substr($string, 0, $chars); $space = mb_strrpos($string, ' '); if ($space < mb_strlen($string) / 2) return $string . $terminate; else return mb_substr($string, 0, $space) . $terminate; } function truncateMickmackusa($string, $max = 50, $terminator = ' …') { $trunc = $max - mb_strlen($terminator, 'UTF-8'); return preg_replace("~(?=.{{$max}})(?:\S{{$trunc}}|.{0,$trunc}(?=\s))\K.+~us", $terminator, $string); } $tests = [ [ 'testCase' => "Answer to the Ultimate Question of Life, the Universe, and Everything.", // 50th char ---------------------------------------------------^ 'expected' => "Answer to the Ultimate Question of Life, the …", ], [ 'testCase' => "A single line of text to be followed by another\nline of text", // 50th char ----------------------------------------------------^ 'expected' => "A single line of text to be followed by another …", ], [ 'testCase' => "âãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝ", // 50th char ---------------------------------------------------^ 'expected' => "âãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđ …", ], [ 'testCase' => "123456789 123456789 123456789 123456789 123456789", // 50th char doesn't exist -------------------------------------^ 'expected' => "1234567890123456789012345678901234567890123456789", ], [ 'testCase' => "Hello worldly world", // 50th char doesn't exist -------------------------------------^ 'expected' => "Hello worldly world", ], [ 'testCase' => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYXZ1234567890", // 50th char ---------------------------------------------------^ 'expected' => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV …", ], ]; foreach ($tests as ['testCase' => $testCase, 'expected' => $expected]) { echo "\tSample Input:\t\t$testCase\n"; echo "\n\ttruncateGumbo:\t\t" , truncateGumbo($testCase); echo "\n\ttruncateGordon:\t\t" , truncateGordon($testCase); echo "\n\ttruncateSoapBox:\t" , truncateSoapBox($testCase); echo "\n\ttruncateMickmackusa:\t" , truncateMickmackusa($testCase); echo "\n\tExpected Result:\t{$expected}"; echo "\n-----------------------------------------------------\n"; }

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.4.120.0110.00820.58
8.4.110.0120.00922.48
8.4.100.0130.00918.19
8.4.90.0160.00518.28
8.4.80.0040.00518.04
8.4.70.0140.00618.19
8.4.60.0070.01018.12
8.4.50.0120.00820.16
8.4.40.0050.00319.89
8.4.30.0000.01918.79
8.4.20.0100.00318.82
8.4.10.0060.00318.14
8.3.250.0110.00820.84
8.3.240.0130.00716.89
8.3.230.0070.00217.24
8.3.220.0110.00819.02
8.3.210.0080.01117.12
8.3.200.0120.00916.99
8.3.190.0070.00217.10
8.3.180.0100.01016.91
8.3.170.0040.01316.77
8.3.160.0090.00019.03
8.3.150.0070.01117.18
8.3.140.0080.00817.14
8.3.130.0080.01118.69
8.3.120.0030.00620.66
8.3.110.0080.00017.25
8.3.100.0000.00918.99
8.3.90.0000.01526.77
8.3.80.0100.00317.00
8.3.70.0120.00316.88
8.3.60.0110.00818.93
8.3.50.0060.01018.70
8.3.40.0000.01419.09
8.3.30.0060.01019.21
8.3.20.0070.00021.20
8.3.10.0030.00622.05
8.3.00.0120.00620.61
8.2.290.0110.00820.55
8.2.280.0110.00918.64
8.2.270.0070.01319.15
8.2.260.0120.00618.96
8.2.250.0040.00416.92
8.2.240.0040.01117.20
8.2.230.0050.00520.94
8.2.220.0060.00324.06
8.2.210.0040.00426.77
8.2.200.0060.00318.91
8.2.190.0090.00616.75
8.2.180.0070.01418.92
8.2.170.0150.00322.96
8.2.160.0000.01522.20
8.2.150.0060.00324.18
8.2.140.0050.00324.66
8.2.130.0080.00026.16
8.2.120.0000.00821.23
8.2.110.0040.00722.23
8.2.100.0100.00318.28
8.2.90.0000.00820.64
8.2.80.0040.00418.04
8.2.70.0030.00618.04
8.2.60.0080.00318.16
8.2.50.0040.00418.10
8.2.40.0080.00021.31
8.2.30.0030.00519.42
8.2.20.0020.00518.24
8.2.10.0080.00019.61
8.2.00.0020.00519.30
8.1.330.0120.00922.16
8.1.320.0170.00316.74
8.1.310.0110.00718.43
8.1.300.0060.00320.55
8.1.290.0060.00330.84
8.1.280.0070.00725.92
8.1.270.0060.00323.97
8.1.260.0090.00026.35
8.1.250.0000.00828.09
8.1.240.0060.00323.88
8.1.230.0090.00321.07
8.1.220.0030.00518.01
8.1.210.0090.00018.84
8.1.200.0040.00417.73
8.1.190.0000.00917.72
8.1.180.0090.00018.10
8.1.170.0030.00618.65
8.1.160.0030.00819.04
8.1.150.0080.00019.07
8.1.140.0050.00320.24
8.1.130.0070.00020.20
8.1.120.0040.00417.65
8.1.110.0080.00017.57
8.1.100.0030.00517.60
8.1.90.0000.00717.60
8.1.80.0040.00417.75
8.1.70.0070.00017.73
8.1.60.0040.00417.80
8.1.50.0030.00517.73
8.1.40.0040.00417.67
8.1.30.0060.00317.92
8.1.20.0100.00017.74
8.1.10.0060.00317.82
8.1.00.0040.00417.72
8.0.300.0030.00619.87
8.0.290.0080.00017.13
8.0.280.0000.00718.48
8.0.270.0000.00717.37
8.0.260.0030.00320.15
8.0.250.0000.00817.20
8.0.240.0070.00417.30
8.0.230.0070.00017.21
8.0.220.0000.00717.32
8.0.210.0070.00017.22
8.0.200.0000.00817.32
8.0.190.0040.00417.22
8.0.180.0050.00317.32
8.0.170.0070.00317.28
8.0.160.0000.00717.22
8.0.150.0000.00717.10
8.0.140.0060.00317.21
8.0.130.0000.00613.76
8.0.120.0000.00917.30
8.0.110.0060.00317.34
8.0.100.0000.00817.37
8.0.90.0000.00817.23
8.0.80.0080.00717.30
8.0.70.0070.00017.35
8.0.60.0040.00417.18
8.0.50.0000.00817.39
8.0.30.0120.01217.46
8.0.20.0040.01617.18
8.0.10.0000.00817.19
8.0.00.0200.00316.94
7.4.330.0000.00616.79
7.4.320.0070.00016.91
7.4.300.0000.00716.77
7.4.290.0040.00417.02
7.4.280.0000.00816.98
7.4.270.0070.00316.91
7.4.260.0070.00013.60
7.4.250.0040.00416.84
7.4.240.0060.00316.80
7.4.230.0030.00316.72
7.4.220.0030.00416.82
7.4.210.0080.00916.94
7.4.200.0000.00717.05
7.4.130.0100.00916.97
7.4.120.0120.00916.81
7.4.110.0130.00616.79
7.4.100.0120.00916.89
7.4.90.0150.00416.77
7.4.80.0070.01319.39
7.4.70.0130.01017.06
7.4.60.0000.01816.89
7.4.50.0080.00516.66
7.4.40.0070.01116.76
7.4.10.0100.00715.19
7.4.00.0050.01115.18
7.3.330.0090.00316.60
7.3.320.0000.00513.71
7.3.310.0070.00016.71
7.3.300.0030.00316.82
7.3.290.0090.00716.77
7.3.280.0150.00316.76
7.3.260.0120.00816.76
7.3.240.0120.01116.84
7.3.230.0070.01116.91
7.3.210.0070.01016.75
7.3.200.0110.00616.93
7.3.190.0090.00916.74
7.3.180.0100.00716.69
7.3.170.0160.00416.87
7.3.160.0030.01416.92
7.3.130.0040.01515.39
7.3.120.0090.00915.18
7.3.110.0100.01014.97
7.3.100.0080.00714.99
7.3.90.0030.01515.23
7.3.80.0060.00715.00
7.3.70.0090.00615.19
7.3.60.0080.01115.09
7.3.50.0100.00614.99
7.3.40.0070.01114.95
7.3.30.0080.00815.11
7.3.20.0080.00915.79
7.3.10.0080.00815.99
7.3.00.0080.00815.79
7.2.330.0110.00716.94
7.2.320.0070.01416.99
7.2.310.0110.00516.95
7.2.300.0120.01216.91
7.2.290.0060.01216.90
7.2.260.0100.01115.26
7.2.250.0030.01615.09
7.2.240.0080.00815.41
7.2.230.0060.01015.18
7.2.220.0070.00915.26
7.2.210.0050.00815.37
7.2.200.0020.01415.20
7.2.190.0100.00715.23
7.2.180.0050.01315.20
7.2.170.0050.01415.32
7.2.160.0070.00915.35
7.2.150.0140.00516.22
7.2.140.0050.01016.14
7.2.130.0070.01216.30
7.2.120.0090.00716.17
7.2.110.0070.01016.17
7.2.100.0060.00916.40
7.2.90.0080.01016.32
7.2.80.0090.01216.30
7.2.70.0150.00916.17
7.2.60.0060.01016.24
7.2.50.0140.00116.21
7.2.40.0060.01016.16
7.2.30.0090.00916.28
7.2.20.0030.01316.13
7.2.10.0050.01216.23
7.2.00.0080.01016.07
7.1.330.0030.01016.02
7.1.320.0030.01215.21
7.1.310.0060.01014.91
7.1.300.0030.01014.94
7.1.290.0110.00715.09
7.1.280.0110.00715.01
7.1.270.0070.00915.02
7.1.260.0090.00915.15
7.1.250.0070.00915.05
7.1.240.0070.00316.18
7.1.230.0040.01116.13
7.1.220.0080.00815.91
7.1.210.0030.01016.16
7.1.200.0140.00415.75
7.1.190.0030.01015.92
7.1.180.0060.00916.15
7.1.170.0100.00316.14
7.1.160.0070.00716.13
7.1.150.0050.00516.18
7.1.140.0130.00415.87
7.1.130.0070.00716.02
7.1.120.0000.01016.07
7.1.110.0000.01316.19
7.1.100.0040.01116.08
7.1.90.0030.01015.96
7.1.80.0090.00316.15
7.1.70.0030.01015.96
7.1.60.0040.00415.94
7.1.50.0100.00315.86
7.1.40.0070.01116.10
7.1.30.0030.01016.05
7.1.20.0030.00616.03
7.1.10.0040.00716.09
7.1.00.0070.00716.15

preferences:
28.86 ms | 403 KiB | 5 Q