PHP Tutorial - PHP chunk_split() Function






Definition

The chunk_split() function splits a string into a series of smaller parts.

Syntax

PHP chunk_split() function has the following syntax.

chunk_split(string,length,end)

Parameter

ParameterIs Required Description
stringRequired. String to split
lengthOptional.A number that defines the length of the chunks. Default is 76
endOptional. A string that defines what to place at the end of each chunk. Default is \r\n

Return

PHP chunk_split() Function returns the split string.





Note

This function does not alter the original string.

Example 1

Split the string after each character and add a "." after each split:


<?php
$str = "Hello world from java2s.com!";
echo chunk_split($str,1,".");
?>

The code above generates the following result.

Example 2

Split the string after each sixth character and add a "..." after each split:


<?php
$str = "Hello world from java2s.com!";
echo chunk_split($str,6,"...");
?>

The code above generates the following result.