Chunk_split: How to Split String in PHP?

In this tutorial, you will learn how to split strings in PHP using the chunk_split() function.

PHP chunk_split() Function

Chunk_split is a built-in PHP function that splits a string into smaller chunks of a specified length. The function returns the original string with a delimiter added between each chunk. The delimiter is specified as the second argument to the function.

Syntax of chunk_split() function:

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

chunk_split(string $str, int $chunk_len = 76, string $end = "\r\n"): string

Parameters of chunk_split() function

The chunk_split() function takes three parameters:

  1. $str: The string to be split.
  2. $chunk_len: The length of each chunk. By default, this is set to 76.
  3. $end: The separator to be inserted between each chunk. By default, this is set to “\r\n” which is the standard line break for email messages.

Return value:

The chunk_split() function returns the modified string as a string.

Example 1: Splitting a string into chunks of 5 characters with a hyphen separator.

Let’s see how to split a string using chunk_split. Suppose we have a string “Hello, world!”. We want to split it into chunks of 3 characters and add a “-” between each chunk. Here’s how we can do it:

<?php
$str = "Hello World";
$chunked_str = chunk_split($str, 5, "-");
echo $chunked_str;
?>

The output of the above code is:

Hello- World-

The above given code is a PHP script that performs the following actions:

  1. The variable $str is assigned the string value “Hello World”.
  2. The chunk_split() function is called with three arguments:
    • The first argument is $str, the string to be chunked.
    • The second argument is 5, which specifies the length of each chunk.
    • The third argument is "-", which specifies the character to insert between each chunk. The chunk_split() function breaks up the string $str into chunks of length 5, and inserts a hyphen character “-” between each chunk.
  3. The resulting string is assigned to the variable $chunked_str.
  4. The echo statement outputs the value of $chunked_str to the screen.

So when the code is executed, it will output the string “Hello- World” (with a space after the hyphen), since the original string “Hello World” was split into two chunks of length 5, with a hyphen between them.

Example 2: Splitting a string into chunks of 10 characters with a line break separator

Here’s an example of how to use chunk_split to split string into chunks of 10 characters with a line break separator:

<?php
$str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean euismod, ante nec gravida blandit, odio dolor aliquet ipsum, eu dapibus nunc nulla vitae massa.";
$chunked_str = chunk_split($str, 10, "\r\n");
echo $chunked_str;
?>

Example 3: Splitting a string into chunks of 3 characters without a separator.

Here is an example of split a string into chunks of 3 characters without a separator:

<?php
$str = "abcdefg";
$chunked_str = chunk_split($str, 3, "");
echo $chunked_str;
?>

The given code is a PHP script that demonstrates the use of the chunk_split() function.

The chunk_split() function is used to split a string into smaller chunks of a specified length, and can be useful for formatting strings in various ways. It takes three arguments:

  1. The first argument is the string to be chunked, which in this case is “abcdefg”.
  2. The second argument is the chunk size, which is set to 3 in this example. This means that the string will be split into chunks of 3 characters each.
  3. The third argument is an optional separator string that will be inserted between the chunks. In this case, the separator is set to an empty string, which means that no separator will be inserted between the chunks.

After calling the chunk_split() function with the specified arguments, the resulting string is stored in the $chunked_str variable. The value of $chunked_str is then outputted to the screen using the echo statement.

Therefore, when the script is executed, the output will be the string “abc def g”, with spaces inserted between each group of three characters.

Example 4: how to split number from string in PHP

However, if you still want to use chunk_split to split a number from a string, you can try the following approach:

$string = "Hello 12345 world";
$chunks = str_split($string, 5); // split into chunks of 5 characters
$number = "";
$remaining = "";

foreach ($chunks as $chunk) {
if (is_numeric($chunk)) {
$number .= $chunk;
} else {
$remaining .= $chunk;
}
}

echo "Number: $number\n";
echo "Remaining: $remaining\n";

So, if the original string was “Hello 12345 world”, the code would output:

Number: 12345
Remaining: Hello world

because the numeric chunk “12345” is extracted into the $number variable and the non-numeric chunks “Hello ” and ” world” are concatenated into the $remaining variable.

Conclusion

Chunk_split is a useful function for splitting a string into smaller chunks of a specified length. It is commonly used when working with email headers or when formatting data that needs to be split into multiple lines.

Recommended Posts:

  1. Autocomplete Search Box in PHP MySQL
  2. Compare Arrays PHP | PHP array_diff() Function
  3. Get, Write, Read, Load, JSON File from Url PHP
  4. Functions: Remove First Character From String PHP
  5. Remove Specific/Special Characters From String In PHP
  6. How to Replace First and Last Character From String PHP
  7. Reverse String in PHP
  8. Array Push, POP PHP | PHP Array Tutorial
  9. PHP Search Multidimensional Array By key, value and return key
  10. json_encode()- Convert Array To JSON | Object To JSON PHP
  11. PHP remove duplicates from multidimensional array
  12. PHP Remove Duplicate Elements or Values from Array PHP
  13. Get Highest Value in Multidimensional Array PHP
  14. PHP Get Min or Minimum Value in Array
  15. Convert CSV to JSON PHP – PHP Tutorial
  16. PHP String to Uppercase, Lowercase & First Letter Uppercase

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *