PHP chunk_split() Function
The chunk_split() function in PHP splits a string into a series of smaller parts.
Syntax
1 2 3 |
chunk_split(string,length,end) |
Parameter
string(required)- It specifies the string to split.
length(optional)- This parameter represents a number that defines the length of the chunks. Its default value is 76.
end(optional)- This parameter signifies a string that defines what to place at the end of each chunk and its default value is \r\n.
Return
This function returns the chunked string.
Example 1
1 2 3 4 5 6 7 8 9 10 |
<?php //initializing the string $str = "Hello World"; //printing the original string echo "Original string: ".$str; // spliting the string into smaller chunks and place'.'' at the end of each chunk echo "\nAfter 'chunk_split()' function\nNew string: ".chunk_split($str,6,"..."); ?> |
Output
1 2 3 4 5 |
Original string: Hello World After 'chunk_split()' function New string: Hello ...World... |
Example 2
1 2 3 4 5 6 7 8 9 10 |
<?php //initializing the string $str = "HelloWorld"; //printing the original string echo "Original string: ".$str; // passing the length to be one echo "\nAfter 'chunk_split()' function\nNew string: ".chunk_split($str,1,"..."); ?> |
Output
1 2 3 4 5 |
Original string: HelloWorld After 'chunk_split()' function New string: H...e...l...l...o...W...o...r...l...d... |
Example 3
1 2 3 4 5 6 7 8 9 10 11 |
<?php //initializing the string $str = "Hello World"; //printing the original string echo "Original string: ".$str; // passing the length to be zero // will return an error as the Chunk length should be greater than zero echo "\nAfter 'chunk_split()' function\nNew string: ".chunk_split($str,0,"..."); ?> |
Output
1 2 3 |
PHP Warning: chunk_split(): Chunk length should be greater than zero in /workspace/Main.php on line 7 |