3v4l.org

run code in 300+ PHP versions simultaneously
<?php function previewTotalLength($labels){ $count = mb_strlen('rfc-vote ') - 2; foreach($labels as $label){ $countEllipsis = $label['trim'] ? 1 : 0; $count += $label['length'] - $label['trim'] + $countEllipsis + 2; } return $count; } function calcTrim($labels, $largestLabelLength, $chars, $stopAtTargetLength){ foreach($labels as $i => $label){ $labels[$i]['trim'] = max((int)floor(($label['length'] - 20) / $largestLabelLength * $chars), 0); if(previewTotalLength($labels) <= $stopAtTargetLength){ $chars++; } } return $labels; } $d = "Typed Properties,PHP Attributes,Functional Interfaces,Closure from callable function,Forbid dynamic calls to scope introspection functions,Callable prototypes"; $labelsList = []; foreach(explode(",", $d) as $label){ $labelsList[] = ['label' => $label, 'length' => mb_strlen($label), 'trim' => 0]; } $largestLabelLength = 0; foreach($labelsList as $label){ if($label['length'] > $largestLabelLength){ $largestLabelLength = $label['length']; } } $strip = 2; while(previewTotalLength($labelsList) > 150){ $labelsList = calcTrim($labelsList, $largestLabelLength, $strip++, 150); } print_r($labelsList); print_r(previewTotalLength($labelsList)); // preview result echo "\n"; echo "rfc-vote "; foreach($labelsList as $label){ $b = mb_substr($label['label'], 0, $label['length'] - $label['trim']); $b .= $label['trim'] ? '…' : ''; $collect[] = $b; } echo implode(', ', $collect);
Output for 5.5.0 - 5.5.35, 5.6.0 - 5.6.21, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Array ( [0] => Array ( [label] => Typed Properties [length] => 16 [trim] => 0 ) [1] => Array ( [label] => PHP Attributes [length] => 14 [trim] => 0 ) [2] => Array ( [label] => Functional Interfaces [length] => 21 [trim] => 0 ) [3] => Array ( [label] => Closure from callable function [length] => 30 [trim] => 5 ) [4] => Array ( [label] => Forbid dynamic calls to scope introspection functions [length] => 53 [trim] => 19 ) [5] => Array ( [label] => Callable prototypes [length] => 19 [trim] => 0 ) ) 150 rfc-vote Typed Properties, PHP Attributes, Functional Interfaces, Closure from callable fun…, Forbid dynamic calls to scope intr…, Callable prototypes

preferences:
101.25 ms | 406 KiB | 214 Q