How to split a string into chunks of two characters each in PHP

1 Answer

0 votes
$string = "abcdefghijk";
$chunkSize = 2;

// Split the string into chunks
$chunks = str_split($string, $chunkSize);

echo "Chunks of two characters:\n";
foreach ($chunks as $chunk) {
    echo $chunk . "\n";
}


  
/*
run:
      
Chunks of two characters:
ab
cd
ef
gh
ij
k

*/

 



answered Mar 30, 2025 by avibootz
...