PHP function chunk_split() – Split a string into smaller chunks

The chunk_split() function divides a string into a sequence of small fragments. chunk_split() adds the character(s) specified in chunk_ending every chunk_length characters. This function is useful for breaking certain types of data into separate lines of a specified length. It’s often used to make base64 encoded data conform to RFC 2045. This function was introduced in PHP3.

Syntax:

string chunk_split(string string, [int chunk_length], [string chunk_ending])

Parameters

Parameter Description
string String to split into chunks
chunk_length Length of the chunks (default 76)
chunk_ending Character(s) to place at the end of each chunk (default \r\n)

Return Values

String with chunk_ending placed every chunk_length

Examples

Example 1:

<?php
// formatting $info by using the RFC 2045 semantics
$new_strng = chunk_split(base64_encode($info)); ?>

Example 2:

$data = "...some long data...";
$converted = chunk_split(base64_encode($data));
Note: This function should not be used for breaking long lines of text into shorter lines for display purposes—use wordwrap() instead.
Related Post