In this article, you will learn how to split a string into small portions. The PHP chunk_split() function split a string into series of small chunks. This function does not change the original string. It returns the chunks from the given string according to the specified length and ending.
What is the syntax of the chunk_split() function in PHP?
|string||The string to split in portions – Required|
|length||The length of the chunks of the string – Optional – Default: 76|
|end||If you want to place some character at the end of each chunk – Default: \r\n|
Examples of the chunk_split() function
Example 1. In this example, we simply split the string after one character and place comma after each one.
<?php $str = "Hello world!"; echo chunk_split($str,1,","); ?>
Example 2. In this example, we split the string into chunks of 6 characters and place ! at the end of the each chunk.
<?php $str = "Hello world!"; echo chunk_split($str,6,"!"); ?>