In PHP, there are several functions available for splitting strings, and one of them is the chunk_split()
function. This function splits a string into smaller chunks of a specified length and inserts a separator between them. In this tutorial, you will learn how to split strings in PHP using the chunk_split()
function.
Splitting a string is a very easy assignment when working with data in PHP. There are various ways to split a string, but one of the most useful functions for this task is chunk_split. This tutorial will cover how to split strings in PHP using the chunk_split()
function with examples.
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:
$str
: The string to be split.$chunk_len
: The length of each chunk. By default, this is set to 76.$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:
- The variable
$str
is assigned the string value “Hello World”. - 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. Thechunk_split()
function breaks up the string$str
into chunks of length 5, and inserts a hyphen character “-” between each chunk.
- The first argument is
- The resulting string is assigned to the variable
$chunked_str
. - 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:
- The first argument is the string to be chunked, which in this case is “abcdefg”.
- 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.
- 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";
The above given is a block of PHP code that operates on a string variable called $string
. The code splits the string into chunks of 5 characters using the str_split()
function, which creates an array of substrings.
Next, the code initializes two empty strings called $number
and $remaining
. The foreach
loop then iterates through each of the chunks in the array, and for each chunk, it checks if the chunk contains only numeric characters using the is_numeric()
function.
If the chunk is numeric, it appends the chunk to the $number
string using the concatenation operator .=
. Otherwise, it appends the chunk to the $remaining
string.
Finally, the code prints out the contents of the $number
and $remaining
strings using the echo
statement.
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. By understanding how to use chunk_split, you can easily split strings in PHP and format your data according to your needs.
Recommended Posts:
- Autocomplete Search Box in PHP MySQL
- Compare Arrays PHP | PHP array_diff() Function
- Get, Write, Read, Load, JSON File from Url PHP
- Functions: Remove First Character From String PHP
- Remove Specific/Special Characters From String In PHP
- How to Replace First and Last Character From String PHP
- Reverse String in PHP
- Array Push, POP PHP | PHP Array Tutorial
- PHP Search Multidimensional Array By key, value and return key
- json_encode()- Convert Array To JSON | Object To JSON PHP
- PHP remove duplicates from multidimensional array
- PHP Remove Duplicate Elements or Values from Array PHP
- Get Highest Value in Multidimensional Array PHP
- PHP Get Min or Minimum Value in Array
- Convert CSV to JSON PHP – PHP Tutorial
- PHP String to Uppercase, Lowercase & First Letter Uppercase