PHP chunk_split Function | Split String to Chunks

PHP chunk_split is an inbuilt function in PHP. It splits the input string into smaller chunks of the specified length. In this article, we will discuss the PHP chunk_split Function. Also, we will discuss a few examples of using it.


string chunk_split($string, $length = 76, $end = “\r\n”)


The PHP chunk_split Function expects three parameters. However, only one parameter is mandatory and two are optional.

  • $string: The first parameter to the function is the input string. It is a mandatory parameter.
  • $length: The second parameter specifies the length of each chunk. It is an optional parameter with default value as 76.
  • $end: The third parameter to the function specifies the line ending sequence. Also, it is an optional parameter.

Return Value

The PHP chunk_split returns a string after dividing it in smaller chunks.

PHP chunk_split Function


Let’s discuss a few examples of using the function.

Example 1: Simple Chunks

For instance, consider a splitting a simple string into smaller chunks.


        //Split the string into chunks of size 3 ending with *
	$inputString = 'Bringing Knowledge Together';
	echo chunk_split($inputString, 3, '*');

In the above example, we split the string into chunks of size 3 ending with *.

Bri*ngi*ng *Kno*wle*dge* To*get*her*

Example 2: Uneven Splitting

The last chunk is always ended with the $end string if the chunk size if uneven.


	$inputString = 'Bringing Knowledge Together';
	echo chunk_split($inputString, 5, '##');


In the above example, the string is split into smaller chunks of length 5. However, the last chunk contains only 2 characters.

Bring##ing K##nowle##dge T##ogeth##er##

Example 3: Chunk Size More than Original Length

If the chunk size is more than the original length of the string, then the function adds the $end string at the end.


	$inputString = 'ABC';
	echo chunk_split($inputString, 5, '##');



In conclusion, we discussed the PHP chunk_split Function. You can read more about it on PHP Official Documentation. Additionally, you can learn about more String Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *