Thursday, September 4, 2025
HomeLanguagesPHP chunk_split() Function

PHP chunk_split() Function

The chunk_split() function is a built-in function in PHP. The chunk_split() function is used to split a string into smaller chunks of a specific length.

Syntax: 

string chunk_split($string, $length, $end)

Parameters: This function accepts three parameters as shown in the above syntax and are described below: 

  1. $string: This parameter specifies a string which is needed to be chunked.
  2. $length: This parameter specifies an integer which specifies the chunk length. That is the length of the chunked parts.
  3. $end: This parameter specifies the line ending sequence.

Return value: This function returns the string splitted into smaller chunks.

Examples: 

Input : string = "neveropen" 
        length = 4
        end = "."
Output: Geek.sfor.Geek.s. 


Input: string = "Twinkle bajaj" 
       length = 2 
       end = "*" 
Output: Tw*in*kl*e *ba*ja*j* 

Below programs illustrate the chunk_split() function in PHP:

Program 1:  

PHP




<?php
// PHP program to illustrate the  
// chunk_split function 
  
$str = "Twinkle bajaj";
echo chunk_split($str, 2, "*");
  
?> 


Output: 

Tw*in*kl*e *ba*ja*j* 

Program 2: 

PHP




<?php
// PHP program to illustrate the  
// chunk_split function 
  
$str = "neveropen";
  
// Returns a string with a '.'
// placed after every four characters.
echo chunk_split($str, 4, ".");
?> 


Output: 

geek.sfor.geek.s. 

Program 3: 

PHP




<?php
// PHP program to illustrate the  
// chunk_split function 
  
$str = "abcd";
echo chunk_split($str, 4, "@@");
?> 


Output: 

abcd@@

Program 4: 

PHP




<?php
// PHP program to illustrate the  
// chunk_split function 
  
// If specified length is more than
// string length, then added at the
// end.
$str = "abcd";
echo chunk_split($str, 10, "@@");
?> 


Output: 

abcd@@

Reference: 
http://php.net/manual/en/function.chunk-split.php
 

RELATED ARTICLES

Most Popular

Dominic
32262 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11857 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS