3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = 'lognstring'; $segmentLength = 3; $totalLength = strlen($string); for ($i = 0; $i < $totalLength; $i += $segmentLength) { $result[] = substr( $string, $i > $totalLength - $segmentLength ? $totalLength - $segmentLength : $i, $segmentLength ); } var_export($result);

preferences:
25.63 ms | 405 KiB | 5 Q