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 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:
- $string: This parameter specifies a string which is needed to be chunked.
- $length: This parameter specifies an integer which specifies the chunk length. That is the length of the chunked parts.
- $end: This parameter specifies the line ending sequence.
Return value: This function returns the string spllited into smaller chunks.
Examples:
Input : string = "geeksforgeeks"
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 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 program to illustrate the // chunk_split function $str = "geeksforgeeks"; // Returns a string with a '.' // placed after every four characters. echo chunk_split($str, 4, "."); ?> |
Output:
geek.sfor.geek.s.
Program 4:
<?php // PHP program to illustrate the // chunk_split function $str = "abcd"; echo chunk_split($str, 4, "@@"); ?> |
Output:
abcd@@
Program 5:
<?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
Recommended Posts:
- How to get the function name from within that function using JavaScript ?
- CSS | rgb() Function
- PHP | Ds\Set contains() Function
- CSS | hsl() Function
- PHP | Ds\Map put() Function
- PHP | Ds\Map xor() Function
- p5.js | arc() Function
- p5.js | sin() function
- PHP Ds\Map first() Function
- p5.js | log() function
- D3.js | d3.set.has() Function
- p5.js | cos() function
- PHP Ds\Map sum() Function
- PHP | pow( ) Function
- p5.js | tan() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



