W3docs

chunk_split()

The chunk_split() function is used to split a string into smaller chunks. The syntax of the chunk_split() function is as follows:

The PHP chunk_split() function splits a string into a series of equal-length pieces and inserts a separator after each piece. It does not break the string into an array — it returns a single new string with the separators baked in. The classic use case is wrapping long, unbroken data (such as Base64-encoded content) into fixed-width lines for email/MIME transmission.

This chapter covers the syntax, every parameter, runnable examples, and the gotchas that trip people up.

Syntax

chunk_split(string $string, int $length = 76, string $separator = "\r\n"): string
ParameterRequiredDefaultDescription
$stringYesThe string to be chunked.
$lengthNo76The length of each chunk, in bytes. Must be 1 or greater.
$separatorNo"\r\n"The string inserted after each chunk.

The function returns the new string. The separator is appended after every chunk — including the last one — so the result always ends with a trailing separator.

The defaults are deliberate: 76 characters with a \r\n (CRLF) line ending is exactly what RFC 2045 recommends for MIME-encoded message bodies.

Basic example

Split a string into chunks of 20 characters using the default \r\n separator:

php— editable, runs on the server

Every 20 characters, chunk_split() inserts a carriage return + line feed (\r\n). In a terminal the \r\n shows up as a line break, so the output looks like this:

Lorem ipsum dolor si
t amet, consectetur 
adipiscing elit. Nul
la at nulla justo, e
get luctus tortor. M
aecenas vel est at m
assa aliquam semper.

Note that each visible line is exactly 20 characters of the original string — the function counts characters, not words, so it will split words in the middle. If you need word-aware wrapping instead, use wordwrap().

Using a custom separator

The third parameter lets you choose what to insert after each chunk. Here we use a single newline ("\n") instead of the default \r\n:

php— editable, runs on the server

The output:

Lorem ipsum dolor si
t amet, consectetur 
adipiscing elit. Nul
la at nulla justo, e
get luctus tortor. M
aecenas vel est at m
assa aliquam semper.

The separator can be any string, not just a line break. With chunk_split("abcdefghij", 4, "-") you get abcd-efgh-ij- — notice the trailing - after the final, shorter chunk.

Real-world use case: wrapping Base64 data

The reason chunk_split() exists is email. Base64 output is one long unbroken line, but MIME bodies must wrap at 76 characters. Combining base64_encode() with chunk_split() produces transmission-ready text:

<?php
$data = "Hello World, this is a longer string to demonstrate chunk_split for MIME like wrapping of base64 data.";
$encoded = base64_encode($data);
echo chunk_split($encoded, 76, "\n");
?>

This breaks the Base64 string into 76-character lines:

SGVsbG8gV29ybGQsIHRoaXMgaXMgYSBsb25nZXIgc3RyaW5nIHRvIGRlbW9uc3RyYXRlIGNodW5r
X3NwbGl0IGZvciBNSU1FIGxpa2Ugd3JhcHBpbmcgb2YgYmFzZTY0IGRhdGEu

Things to watch out for

  • Trailing separator. chunk_split() always appends the separator after the last chunk too. If you do not want it, trim it: rtrim(chunk_split($s, 20), "\r\n").
  • It returns a string, not an array. To split a string into an array of fixed-length pieces, use str_split(). To split on a delimiter, use explode().
  • Length is measured in bytes. With multibyte (UTF-8) text, a chunk may fall in the middle of a multibyte character and corrupt it. chunk_split() is byte-safe only for single-byte data such as ASCII or Base64.
  • $length must be positive. Passing 0 triggers a ValueError (PHP 8+) or a warning and false in older versions.
  • wordwrap() — wrap a string to a given width on word boundaries.
  • str_split() — split a string into an array of equal-length pieces.
  • explode() — split a string into an array using a delimiter.
  • nl2br() — insert HTML line breaks before newlines.

Practice

Practice
Which of the following descriptions offers correct information about the chunk_split() function in PHP?
Which of the following descriptions offers correct information about the chunk_split() function in PHP?
Was this page helpful?