PHP chunk_split() function with example

PHP chunk_split() work: Here, we will find out about the chunk_split() work with an example.

PHP chunk_split() work

chunk_split() work is utilized to part the given string into pieces of little parts, it acknowledges the string restores the pieces of the strings indicated by different parameters.

Syntax:

    chunk_split(string, [chunklen], [end_characters]);

Here,

  • string – is the source string
  • chunklen – is a discretionary parameter, it characterizes the number of characters of the lumps. Its default value is 75.
  • end_characters – is likewise a discretionary parameter, it characterizes the end characters that will be added to each piece, and its default value is “\r\n”.

Example:

    Input: 
    str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Function call: chunk_split(str, 3, "...");
    Output:
    ABCD...EFGH...IJKL...MNOP...QRST...UVWX...YZ... 

PHP Code:

<?php
	$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	$split_str = chunk_split($str);
	echo ("The extracted characters are...\n");
	echo ($split_str);

	$split_str = chunk_split($str, 3);
	echo ("The extracted characters are...\n");
	echo ($split_str);

	$split_str = chunk_split($str, 4, "...");
	echo ("The extracted characters are...\n");
	echo ($split_str);
?>

Output

The extracted characters are...
ABCDEFGHIJKLMNOPQRSTUVWXYZ
The extracted characters are...
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
YZ
The extracted characters are...
ABCD...EFGH...IJKL...MNOP...QRST...UVWX...YZ... 

Leave a Comment