AppDividend
Latest Code Tutorials

PHP chunk_split: How to Split String in PHP

PHP chunk_split() is a string function that splits the string into a series of smaller parts. It does not return the original string.

PHP chunk_split()

PHP chunk_split() is a built-in string function used to split or divide the string into chunks or small parts. The chunk_split() function returns the split string. The function is used to split the string into smaller chunks of a specific length.

Syntax

The syntax of the function is the following.

string chunk_split($string, $length, $end)

Arguments

  1. The $string parameter specifies a string which is needed to be chunked.
  2. The $length parameter specifies an integer which defines the chunk length. That is the length of the chunked parts.
  3. The $end parameter specifies the line ending sequence.

Example

See the code example.

<?php

$str = "BobbyBrown";
echo chunk_split($str, 5, "...");

See the output.

➜  pro php app.php
Bobby...Brown...                                                                
➜  pro

See another example of Friends.

<?php

$centralperk = "FRIENDS";
echo chunk_split($centralperk, 1, ".");

See the output.

➜  pro php app.php
F.R.I.E.N.D.S.                                                                  
➜  pro

Remember, The chunk_split() function doesn’t return an array, but rather a string.

If you want to use it, you would have to convert the string returned by the chunk_split() function into an array.

The result of chunk_split() in PHP 5.0.4 does not match the result in other versions of PHP.

That’s it for this tutorial.

Recommended Posts

  1. PHP count_chars()
  2. PHP strlen()
  3. PHP strtotime()
  4. PHP strpos()
  5. PHP trim()
Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.