3v4l.org

run code in 500+ PHP versions simultaneously
<?php function formatStrings($strings, $positions) { $position = 0; foreach ($strings as $key => $str) { // do we need to move to a new position to display the next string? if ($positions[$key]['position'] > $position) { echo str_pad('', $positions[$key]['position'] - $position - 1); $position = $positions[$key]['position']; } // display the string echo str_pad($str, $positions[$key]['width']); $position += $positions[$key]['width'] - 1; } } $strings = array('ETL','E7954','20181123'); $positions = array(array('position' => 20, 'width' => 9), array('position' => 48, 'width' => 14), array('position' => 64, 'width' => 8)); echo " 1 2 3 4 5 6 7\n"; echo "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"; echo formatStrings($strings, $positions);
Output for 5.6.38, 7.1.0 - 7.1.24, 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.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.6
1 2 3 4 5 6 7 12345678901234567890123456789012345678901234567890123456789012345678901234567890 ETL E7954 20181123

preferences:
92.13 ms | 1509 KiB | 4 Q