Converting a string to an array is a pretty common task when programming in PHP. If you don’t know much about PHP you could easily fall into a situation where you’re manually looping through each character in a string checking for a delimiter or keeping a counter and assembling strings of the same length and assigning the value to a new array. Fortunately PHP has you covered!
Split a string by a string
Splitting a string by a string is a common way to deal with data that has a delimiter separating each value or to be able to break a sentence apart into individual words.
$string = 'One, Two, Three, Four, Five'; $array = explode(', ', $string);
Split a string into chunks
Chunking allows you to split a string into parts of equal length (except the last one which could be shorter). The common use case is when you have a really long string and you want to make it a bit more visually appealing.
$string = '12345abcdefghijklmnopqrstuvwxyz'; $array = str_split($string, 5);