Using PHP chunk_split() to Split Strings into Fixed‑Size Chunks

This article explains PHP's chunk_split() function, detailing its purpose of dividing a string into fixed-size pieces, describing its parameters (body, chunklen, end), return value, and providing a practical example that formats base64‑encoded data according to RFC 2045.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Using PHP chunk_split() to Split Strings into Fixed‑Size Chunks

Daily PHP knowledge sharing introduces the chunk_split() function, which splits a string into smaller chunks.

The function is useful for formatting the output of base64_encode() to comply with RFC 2045, inserting a specified end string after every $chunklen characters.

Parameters

body : the string to be split.

chunklen : the size of each chunk (default 76).

end : the line‑ending sequence to append after each chunk (default "\r\n").

Return value

The function returns the split string.

Example

<?php
// Format $data using RFC 2045 semantics
$new_string = chunk_split(base64_encode($data));
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendBase64chunk_splitrfc2045string-manipulation
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.