PHP chunk_split() Function

What is PHP chunk_split() Function?

If you want to split a long text into a specific smaller length parts, use PHP chunk_split(). You can also add a specific separator after each part too.

Syntax:

chunk_split(string, length, separator)

Parameters:

The Function has 1 required parameter and 2 optional parameters-

string (Required): It specifies the string that you want to split.

length (Optional): It specifies the length of each part (or chunk). The default length is 76.

separator (Optional): It specifies a string to be inserted at the end of each chunk. The default value is “\r\n” (carriage return and newline).

Return Values:

The function returns the chunked string as a new string.

Examples:

Example 1:

<?php
$string = "Jameslearnmysql";
echo chunk_split($string, 5, "<br />");
?>

Output:

James
learn
mysql

Notes on chunk_split() Function:

  • The function doesn’t modify the original string. It returns a new string.
  • The function adds separator at the last chunk too. If you want to remove the trailing separator, use substr() function. see following example-
    ddd

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP chunk_split() Function

convert_uudecode() function is one of the built-in string functions. Use this function to split a string into smaller chunks.

Reference:

https://www.php.net/manual/en/function.chunk-split.php