PHP Function Reference

PHP chunk_split() Function



The PHP chunk_split() function splits a string into smaller chunks. It inserts separator every length characters.

Syntax

chunk_split(string, length, separator)

Parameters

string Required. Specify the string to be chunked.
length Optional. Specify the chunk length. Default is 76.
separator Optional. Specify the line ending sequence. Default is \r\n.

Return Value

Returns the chunked string.

Example:

The example below shows the usage of chunk_split() function.

<?php
$str = "Hello World";

echo chunk_split($str, 6, "... \n");
?>

The output of the above code will be:

Hello ... 
World...    

Example:

Consider one more example where this function is used with base64_encode function. The smaller chunks produced by this function is useful for e.g. converting base64_encode() output to match with RFC 2045 semantics.

<?php
$str = "He is John. He likes to code. He lives in London.";

echo chunk_split(base64_encode($str), 20);
?>

The output of the above code will be:

SGUgaXMgSm9obi4gSGUg
bGlrZXMgdG8gY29kZS4g
SGUgbGl2ZXMgaW4gTG9u
ZG9uLg==

❮ PHP String Reference