chunk_split() : Break string

$str="Welcome to plus2net.com";
echo chunk_split($str,5,'#');
output
Welco#me to# plus#2net.#com#
chunk_split(string, interval, separator)
We can break each string after fixed interval and add string to each breaks.

string : main string to be broken.
interval : The chunk lenght.
separator : string which is to be added after each split.

If interval or adding string is not given then a line break is added at the end of the string.
$str="This is a string and this is line end";
echo chunk_split($str);
The output is same
This is a string and this is line end
However in above line one line break is added at the end, to see the line break you can use the nl2br function like this.
echo nl2br(chunk_split($str))
You can see one line break is added at the end. Let us try another example
$str="This is a string and this is line end";
echo chunk_split($str,5,'*');
Output is here
This *is a *strin*g and* this* is l*ine e*nd* 
After five chars inculding space string is broken and one * is added.

Example: Splitting Numbers for Readability

$number = '1234567890';
echo chunk_split($number, 3, ',');  // Output: 123,456,789,0

Example: Splitting with a Custom Character

$str = "abcdefghij";
echo chunk_split($str, 2, '-');  // Output: ab-cd-ef-gh-ij-

String Functions chop(): remove string from right end
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer