PHP chunk_split() function with example

By IncludeHelp Last updated : December 27, 2023

PHP chunk_split() function

The chunk_split() function is used to split the given string into chunks of small parts, it accepts the string returns the parts of the strings specified by the other parameters.

Syntax

The syntax of the chunk_split() function:

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

Parameters

The parameters of the chunk_split() function:

  • string - is the source string
  • chunklen - is an optional parameter, it defines the number of characters of the chunks. It's default value is 75.
  • end_characters - is also an optional parameter, it defines the end characters that will be added to the each chunk, and its default value is "\r\n".

Return Value

The return value of this method is string, it returns the parts of the strings specified by the other parameters.

Sample Input/Output

Input: 
str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Function call: chunk_split(str, 3, "...");

Output:
ABCD...EFGH...IJKL...MNOP...QRST...UVWX...YZ... 

Example of PHP chunk_split() Function

<?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 output of the above example is:

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... 

To understand the above example, you should have the basic knowledge of the following PHP topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.