PHP 8.5 chunk_split() Function Use With Example

The chunk_split() function in PHP 8.5 works exactly the same as in previous versions. Its primary purpose is to split a string into smaller chunks and insert a separator after each chunk. This is especially useful when formatting long strings, such as wrapping Base64-encoded data to comply with email standards (RFC 2045).

Here is everything you need to know about using it, along with practical examples.

What Does chunk_split() Do?

The function takes a string and breaks it down by a specified length. It does not return an array; instead, it returns a new string with the separator inserted after every chunk. The original string remains unchanged.

The Syntax

The function signature is: chunk_split(string $string, int $length = 76, string $separator = " "): string

Understanding the Parameters

  • $string: The input text you want to split.
  • $length: The number of characters per chunk. The default is 76, which is the recommended line length for MIME-encoded data.
  • $separator: The string added after each chunk. The default is a carriage return and line feed ( ).

Basic Usage and Examples

Here is how you can use the function in your code.

Example 1: The Standard RFC 2045 Use Case

The most common reason to use chunk_split() is to format the output of base64_encode() so it can be safely sent in emails without line length issues.

<?php
$data = "This is a long string that will be encoded and split.";
$encoded = base64_encode($data);

// Split the encoded string into 76-character lines
$formatted = chunk_split($encoded);

echo $formatted;
?>

Output: 

The Base64 string will be broken into multiple lines, each exactly 76 characters long, ending with .

Example 2: Custom Length and Separator

You are not limited to the defaults. You can specify your own chunk size and the character used to separate them. A common use case is grouping numbers or formatting IDs.

<?php
$string = "1234567890";

// Split into chunks of 3, using a hyphen as a separator
$chunked = chunk_split($string, 3, "-");

echo $chunked;
?>

Output: 

123-456-789-0-

Notice that the separator is added even after the last (incomplete) chunk. If you want to remove the trailing separator, you can use rtrim():

echo rtrim($chunked, "-"); // Outputs: 123-456-789-0

Important Notes and Best Practices

The Trailing Separator Behaviour

A common point of confusion is that chunk_split() always adds a trailing separator at the very end of the string, even if the last chunk is shorter than the specified length. If this is not desired, you must manually remove it using functions like rtrim() or substr().

Multibyte Character Warning

The $length parameter counts bytes, not characters. If you are working with UTF-8 text (like Hindi, Arabic, or emojis), you might split a multi-byte character in half, resulting in corrupted text (mojibake). For multi-byte strings, it is safer to use mb_str_split() or wordwrap(), or use a custom function with preg_split() using the u modifier.

4 Views