Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

chunk_split

Splits a string into chunks, inserting a separator after each.

Syntax

chunk_split(string $string, int $length = 76, string $separator = "\r\n"): string

Parameters

ParameterTypeDescription
$stringstringThe input string.
$lengthintOptional. The chunk length.
$separatorstringOptional. The separator.

Return value

stringThe string with separators.

Examples

A card number in groups of four digits:

<?php
 
echo trim(chunk_split('4276123456789012', 4, ' '));

Output:

4276 1234 5678 9012

Notes

The separator is added after the last chunk too — remove it with trim() or rtrim().

See also

  • str_split — Splits a string into pieces of a given length.
  • wordwrap — Wraps a long string at word boundaries.
  • implode — Joins array elements into a string.

In the tutorial

  • Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.

Thanks for reading!

Donate to the site — we work hard for you.