Home » PHP

PHP chunk_split() function with example

PHP chunk_split() function: Here, we are going to learn about the chunk_split() function with example.
Submitted by IncludeHelp, on February 03, 2019

PHP chunk_split() function

chunk_split() function is used to split the given string into chunks of small parts, it accepts the string returns the parts of the strings specified by the other parameters.

Syntax:

    chunk_split(string, [chunklen], [end_characters]);

Here,

  • string - is the source string
  • chunklen - is an optional parameter, it defines the number of characters of the chunks. It's default value is 75.
  • end_characters - is also an optional parameter, it defines the end characters that will be added to the each chunk, and its default value is "\r\n".

Examples:

    Input: 
    str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Function call: chunk_split(str, 3, "...");
    Output:
    ABCD...EFGH...IJKL...MNOP...QRST...UVWX...YZ... 

PHP code:

<?php
	$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	$split_str = chunk_split($str);
	echo ("The extracted characters are...\n");
	echo ($split_str);

	$split_str = chunk_split($str, 3);
	echo ("The extracted characters are...\n");
	echo ($split_str);

	$split_str = chunk_split($str, 4, "...");
	echo ("The extracted characters are...\n");
	echo ($split_str);
?>

Output

The extracted characters are...
ABCDEFGHIJKLMNOPQRSTUVWXYZ
The extracted characters are...
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
YZ
The extracted characters are...
ABCD...EFGH...IJKL...MNOP...QRST...UVWX...YZ... 






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.