Use chunk_split() Function in PHP 7.4 With Example

The `chunk_split()` function in PHP 7.4 is used to split a string into smaller chunks with a specified chunk length and an optional separator between chunks.

Syntax:

chunk_split(string $body, int $chunklen = 76, string $end = "\r\n"): string

– $body: The input string you want to split.
– $chunklen: The length of each chunk (default is 76 characters).
– $end: The string to be added at the end of each chunk (default is `”\r\n”`).

Example:

<?php
// Original string
$str = "ThisIsAVeryLongStringThatNeedsToBeSplitIntoSmallerChunks";

// Split the string into chunks of 10 characters each, with a hyphen ("-") as the separator
$result = chunk_split($str, 10, "-");

// Output the result
echo $result;
?>

Output:

ThisIsAVer-yLongStri-ngThatNeed-sToBeSplit-IntoSmaller-Chunks-