chunk_split()

chunk_split() — Split a string into smaller chunks.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Can be used to split a string into smaller chunks which is useful for e.g. converting base64_encode() output to match RFC 2045 semantics. It inserts separator every length characters.

Signature

chunk_split(string $string, int $length = 76, string $separator = "\r\n"): string

Parameters

ParameterTypeWhat it takes
$stringstringThe string to be chunked.
$lengthintThe chunk length.
$separatorstringThe line ending sequence.

Return value

Returns the chunked string.

Example

A runnable snippet:

$data = 'This is quite a long string, which will get broken up because the line is going to be too long after base64 encoding it.'; // format $data using RFC 2045 semantics $new_string = chunk_split(base64_encode($data)); echo $new_string, PHP_EOL;

Changelog

VersionChange
8.0.0Now throws a ValueError if length is less than 1. Previously, an E_WARNING was raised and false was returned.

See also

str_split(), explode(), wordwrap()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page chunk_split() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.