Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,011 questions

51,958 answers

573 users

How to use chunk_split() to split a string into smaller chunks and insert them into an array in PHP

1 Answer

0 votes
function chunk_split_arr($str, $chunklen) 
{
   $arr = array();
   $len = ceil(strlen($str) / $chunklen);
   for($i = 0; $i < $len; $i++) 
      $arr[] = substr($str, $i * $chunklen, $chunklen);
   
   return $arr;   
}

$s = 'abcdefghij';
 
$arr = chunk_split_arr($s, 3);
 
echo "<pre>";
var_dump($arr);
echo "</pre>";

 
/*
run: 

array(4) {
  [0]=>
  string(3) "abc"
  [1]=>
  string(3) "def"
  [2]=>
  string(3) "ghi"
  [3]=>
  string(1) "j"
}

*/

 



answered Jun 4, 2016 by avibootz

Related questions

4 answers 263 views
1 answer 108 views
2 answers 324 views
324 views asked Jul 4, 2015 by avibootz
1 answer 124 views
1 answer 136 views
1 answer 102 views
2 answers 123 views
...