The chunk_split Function
The chunk_split
function splits a string into parts of a specified length and inserts a given separator between them.
It takes the source string as the first parameter, the length of the fragments as the second, and the separator as the third (default is '\r\n'
). Returns the modified string.
Syntax
chunk_split(string, length, [end]);
Example
Let's split the string '1234567890'
into fragments of 3
characters with the separator '-'
:
<?php
echo chunk_split('1234567890', 3, '-');
?>
Code execution result:
'123-456-789-0-'
Example
Let's split the string 'abcdef'
into fragments of 2
characters with the default separator:
<?php
echo chunk_split('abcdef', 2);
?>
Code execution result:
'ab\r\ncd\r\nef\r\n'