PHP chunk_split() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, chunk_split() splits a string into smaller chunks and inserts a separator after each chunk.

The string is required. Chunk length and ending are optional. A common use is formatting Base64 output.

Syntax

Call chunk_split on the string. Start with this line.

PHP
$out = chunk_split($data, 76, "\r\n");
CallWhat it does
chunk_split("ABCDEF", 2, "-")Inserts - every 2 characters.
chunk_split(base64_encode($bin))Formats Base64 for email-style lines.

Default length is 76 and default ending is \r\n.

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

Parameters

ParameterDescription
stringRequired. The input string to split.
lengthOptional. Chunk size. Default 76.
separatorOptional. Text inserted after each chunk. Default \r\n.

Return Value

Return ValueDescription
stringA new string with separators inserted.

PHP Examples

Example 1 — Insert a Dash Every Two Characters

The everyday call: set length and separator yourself.

PHP
<?php
$text = "ABCDEF";
$result = chunk_split($text, 2, "-");

echo $result;
// AB-CD-EF-

How It Works

  1. Take the string. "ABCDEF" has six characters.
  2. Cut every 2. Chunks are AB, CD, EF.
  3. Add - after each. Including after EF.
  4. One string back. Result is "AB-CD-EF-", not an array.

Example 2 — MIME-Style Line Breaks

Defaults use length 76 and "\r\n". Here we use a short length so the idea is easy to see.

PHP
<?php
$body = "AAAAABBBBBCCCCC";
$wrapped = chunk_split($body, 5, "\n");

echo $wrapped;
/*
AAAAA
BBBBB
CCCCC
*/

How It Works

  1. Choose a line width. Length 5 stands in for the usual 76.
  2. Use a newline separator. Each chunk becomes its own line.
  3. Even last line. Exact multiples still get a trailing newline.
  4. Mail tip. Real MIME bodies often keep the default "\r\n".

Example 3 — Remove the Trailing Separator

When you do not want the final separator, trim it after chunk_split.

PHP
<?php
$raw = chunk_split("12345678", 3, ":");
$clean = rtrim($raw, ":");

echo $raw . PHP_EOL;
echo $clean . PHP_EOL;
// 123:456:78:
// 123:456:78

How It Works

  1. Chunk first. Length 3 → 123, 456, 78.
  2. Separators everywhere. Raw result ends with :.
  3. rtrim the separator. Removes trailing colons only.
  4. Keep mid separators. Joins between chunks stay intact.

Frequently Asked Questions

chunk_split() breaks a string into fixed-size pieces and inserts a separator after each piece. It returns one combined string—not an array. The last chunk may be shorter, and the separator is still appended after it.
string chunk_split(string $string, int $length = 76, string $separator = "\r\ "). Pass the input, the chunk size (default 76), and the separator after every chunk (default CRLF).
That is by design. Every chunk—including the final partial one—gets the separator. Trim it afterward, or use str_split() plus implode() if you need different joining rules.
chunk_split() returns one string with separators inserted. str_split() returns an array of chunks with no separator. Use chunk_split() for formatted output; use str_split() when you process chunks one by one.
76 characters per line is a common convention for email and MIME-encoded content. The default separator "\r\ " matches CRLF line endings used in many mail formats.

Next: convert_cyr_string()

Convert strings between Cyrillic character sets.

convert_cyr_string() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful