3v4l.org

run code in 300+ PHP versions simultaneously
<?php function truncate($input, $maxWords, $maxChars){ $words = preg_split('/\s+/', $input); $words = array_slice($words, 0, $maxWords); $words = array_reverse($words); $chars = 0; $truncated = array(); while(count($words) > 0) { $fragment = trim(array_pop($words)); $chars += strlen($fragment); if($chars > $maxChars){ if(!$truncated){ $truncated[]=substr($fragment, 0, $maxChars - $chars); } break; } $truncated[] = $fragment; } $result = implode($truncated, ' '); $wordCount = count($truncated); // Handle Case where character count is violated if ($maxLength && $chars > $maxLength){ $result = substr($result, 0, ceil($maxLength/2)."...".substr($results, -1 * floor($maxLength/2); } elseif($wordCount && $wordCount > $maxWords){ // deal with too many words $result = implode(" ", array_slice($truncated, 0, ceil($wordCount/2))." ... ". implode(" ", array_slice($truncated, $wordCount - floor($wordCount/2) -1)) } return $result; } $thing = truncate('the quick brown fox jumped over the lazy dog', 8, 16); echo $thing;

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)
5.4.150.0080.04212.41
5.4.140.0080.04612.10
5.4.130.0060.04212.08
5.4.120.0050.04412.04
5.4.110.0080.04812.04
5.4.100.0060.04212.04
5.4.90.0090.04512.04
5.4.80.0060.04612.04
5.4.70.0050.04412.03
5.4.60.0070.04112.03
5.4.50.0080.04112.03
5.4.40.0070.04112.02
5.4.30.0090.04212.02
5.4.20.0120.04312.02
5.4.10.0090.04612.02
5.4.00.0070.04211.51
5.3.250.0080.04312.72
5.3.240.0080.04312.72
5.3.230.0100.04612.71
5.3.220.0060.04912.68
5.3.210.0090.04812.68
5.3.200.0080.04612.68
5.3.190.0080.04312.67
5.3.180.0090.04112.67
5.3.170.0090.04212.67
5.3.160.0070.04412.67
5.3.150.0110.04212.67
5.3.140.0090.04212.66
5.3.130.0090.04512.66
5.3.120.0080.04812.66
5.3.110.0070.04712.66
5.3.100.0080.04312.14
5.3.90.0070.04612.12
5.3.80.0070.04512.10
5.3.70.0090.04312.10
5.3.60.0090.04412.09
5.3.50.0110.03912.04
5.3.40.0110.04012.04
5.3.30.0100.04511.99
5.3.20.0070.04411.78
5.3.10.0080.04111.75
5.3.00.0080.04111.73

preferences:
137.38 ms | 1398 KiB | 8 Q