Skip to content

Instantly share code, notes, and snippets.

@NicTorgersen
Last active May 12, 2022 20:19
Show Gist options
  • Save NicTorgersen/da33e7f5b83be051a61d220b1470e697 to your computer and use it in GitHub Desktop.
Save NicTorgersen/da33e7f5b83be051a61d220b1470e697 to your computer and use it in GitHub Desktop.
Split a string in PHP into segments as array
<?php
$output = [];
$lengthPerSegment = 40;
$string = "Hello, this is a string.";
foreach (str_split($string) as $index => $character) {
if ($index % $lengthPerSegment === 0) {
$output[] = substr($string, $index, $lengthPerSegment);
}
}
echo print_r($output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment