Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Split string into chunks of 8, how do I do this?

I have binary basically, say it's 300 in length. How would I split (much like using explode) it into 8 bit chunks? I look at chunk_split() but it only seems to have an 'end' parameter, not an option to put it into an array.. Or can it be socketed into an array?

The end 8 digits can be below 8 (in case a miscopy from someone, and it's 4) so no validation is needed, just consistantly in 8 number chunks from the start to end.

like image 371
oni-kun Avatar asked Feb 03 '10 10:02

oni-kun


People also ask

How do you split a string in PHP?

explode() is a built in function in PHP used to split a string in different strings. The explode() function splits a string based on a string delimiter, i.e. it splits the string wherever the delimiter character occurs. This functions returns an array containing the strings formed by splitting the original string.

Which function is used to divide the string into smaller chunks in PHP?

The chunk_split() function splits a string into a series of smaller parts.

What is split function in PHP?

PHP - Function split() The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string.

What PHP function would you use to join array elements into a string using a separator string )?

PHP | join() Function The join() function is built-in function in PHP and is used to join an array of elements which are separated by a string.


1 Answers

str_split:

$chunks = str_split($data, 8);
like image 69
strager Avatar answered Sep 29 '22 08:09

strager