Search for:


PHP chunk_split function

The chunk_split() function in PHP is used to split a string into smaller chunks by inserting a delimiter between them. It can be useful for formatting data or dividing long strings into more manageable parts.

The syntax of the chunk_split() function is as follows:

Example for PHP chunk_split function

chunk_split(string $str, int $chunk_length = 76, string $end = "\r\n"): string

The function accepts three parameters:

  • $str (required): The input string that you want to split into chunks.
  • $chunk_length (optional): The maximum length of each chunk. By default, it is set to 76 characters.
  • $end (optional): The delimiter that will be inserted between the chunks. By default, it is set to the carriage return and newline characters (“\r\n”).

The function divides the input string into chunks of the specified length and inserts the delimiter at the end of each chunk. The last chunk may have a shorter length if the remaining characters are less than the specified chunk length.

Here’s an example usage of the chunk_split() function:

$input_string = "Hello how are you";
$chunked_string = chunk_split($input_string, 10, "--");

echo $chunked_string;


Hello how-- are you--

In this example, the input string is divided into chunks of length 10, and the delimiter “–” is inserted at the end of each chunk. As a result, the output string consists of chunks with the delimiter in between.

Please note that the chunk_split() function does not modify the original string; it returns a new string with the desired formatting.