3v4l.org

run code in 300+ PHP versions simultaneously
<?php $rfcs = ['Typed Properties', 'PHP Attributes', 'Functional Interfaces', 'Closure from callable function', 'Forbid dynamic calls to scope introspection functions', 'Callable prototypes']; $numberOfCharsBeforeTrimRequired = 150; $perRFC = (int)($numberOfCharsBeforeTrimRequired / count($rfcs)); $done = []; do { $lastTrimNeeded = count($rfcs); $remaining = 0; foreach ($rfcs as $i => $rfc) { if (strlen($rfc) <= $perRFC) { $done[] = $rfc; $remaining += $perRFC - strlen($rfc); unset($rfcs[$i]); } } $perRFC += (int)($remaining / count($rfcs)); } while ($lastTrimNeeded > count($rfcs)); foreach ($rfcs as $rfc) { $done[] = substr($rfc, 0, $perRFC); } // get total length to check algo $totalLen = 0; foreach ($done as $rfc) { $totalLen += strlen($rfc); } var_dump($done, $totalLen);

preferences:
15.82 ms | 405 KiB | 5 Q