Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

PHP String chunk_split() Function

Posted in PHP Tutorial
Updated on Nov 22, 2024
By Mari Selvan
👁ī¸ 114 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
PHP String chunk_split() Function

Photo Credit to CodeToFun

🙋 Introduction

In PHP, manipulating strings is a common task, and various functions are available to assist in these operations.

The chunk_split() function is one such function that is used to split a string into smaller chunks and insert a delimiter between them.

This tutorial will explore the usage and functionality of the chunk_split() function in PHP.

💡 Syntax

The signature of the chunk_split() function is as follows:

Syntax
Copied
Copy To Clipboard
string chunk_split(string $string, int $length = 76, string $end = "\r\n");
  • $string: The input string to be split into chunks.
  • $length: The length of each chunk. The default value is 76.
  • $end: The string to be inserted at the end of each chunk. The default value is "\r\n" (Carriage Return + Line Feed).

📄 Example

Let's delve into an example to illustrate how the chunk_split() function works.

chunk_split.php
Copied
Copy To Clipboard
<?php

$inputString = "HelloWorld123";

// Split the string into chunks of length 4 with a dash at the end of each chunk
$chunkedString = chunk_split($inputString, 4, '-');

// Output the result
echo $chunkedString;

?>

đŸ’ģ Output

Output
Hell-oWor-ld12-3-

🧠 How the Program Works

In this example, the chunk_split() function is used to split the string "HelloWorld123" into chunks of length 4 with a dash at the end of each chunk.

↩ī¸ Return Value

The chunk_split() function returns the modified string after splitting it into chunks.

📚 Common Use Cases

The chunk_split() function is useful in scenarios where you need to format or display data in a specific way. For example, it's commonly used in MIME email messages to ensure that lines are limited to a certain length.

📝 Notes

  • The $length parameter determines the maximum length of each chunk. The function adds the specified $end string to the end of each chunk.
  • If the input string's length is not a multiple of the specified length, the last chunk will be shorter than the specified length.
  • The default value of $end is commonly used for formatting email messages, but you can change it to fit your specific requirements.

đŸŽĸ Optimization

The chunk_split() function is efficient for its intended purpose. If you have specific performance concerns, you may want to measure the impact of this function on your application's performance and explore alternatives if needed.

🎉 Conclusion

The chunk_split() function in PHP is a handy tool for breaking up strings into chunks and inserting a delimiter between them. It offers flexibility in formatting and is particularly useful for tasks like constructing formatted messages or handling data with specific line-length requirements.

Feel free to experiment with different strings, lengths, and delimiters to explore the behavior of the chunk_split() function in various scenarios. Happy coding!

👨‍đŸ’ģ Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
10 months ago

If you have any doubts regarding this article (PHP String chunk_split() Function), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy