Last active
May 12, 2022 20:19
-
-
Save NicTorgersen/da33e7f5b83be051a61d220b1470e697 to your computer and use it in GitHub Desktop.
Split a string in PHP into segments as array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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