Definition and Usage
- PHP Version
- 5+
The str_split() function splits a string into an array by a specified length.
The elements of the returned array consist of substrings split to the specified length, making this function particularly useful for splitting strings into manageable segments for easier processing.
Features
- The original string remains unchanged.
- The split length is measured in bytes (1 byte). Consequently, multi-byte strings may yield unexpected results.
Basic Example
$str = 'Hello World!';
// Split the string into segments of 1 character each
$arr1 = str_split($str, 1);
// Split the string into segments of 3 characters each
$arr2 = str_split($str, 3);
print_r($arr1);
/*
Output:
Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
[5] =>
[6] => W
[7] => o
[8] => r
[9] => l
[10] => d
[11] => !
)
*/
print_r($arr2);
/*
Output:
Array
(
[0] => Hel
[1] => lo
[2] => Wor
[3] => ld!
)
*/
Pro Tip!
The preg_split() function splits a string into an array based on regular expression patterns.
Pro Tip!
The explode() function splits a string into an array based on a delimiter.
Syntax
str_split(string $string, int $length = 1): array
Parameters
$string |
Required. The input string to be split. |
|---|---|
$length |
Optional. Specifies the length of each substring. If this parameter is omitted, the default value of 1 is assigned. |
Return Values
The function splits the string into substrings of the length specified by the $length parameter and returns the result as an array. If the input string cannot be divided evenly by the specified length, the final element may be shorter than the rest.
The string length is measured in bytes (1 byte), meaning that multibyte strings are not supported.
The multibyte-safe version of this function, mb_str_split(), measures string length in characters, ensuring reliable performance with multibyte strings.
Changelog
| Version | Description |
|---|---|
| 8.2.0 | Prior to PHP 8.2.0, if the input string was empty (''), the function returned an array containing a single empty string. From 8.2.0 onwards, it returns an empty array. |
| 8.0.0 | Prior to PHP 8.0.0, the function returned false if the $length parameter was less than 1. As of 8.0.0, a ValueError is thrown. |
Practical Examples
The str_split() function allows for easy division of strings into substrings, making it highly effective for various practical applications.
Let's explore a few examples.
Reversing a String
To reverse a string, you can decompose it into an array using the str_split() function, then reverse the order of the elements with the array_reverse() function.
The reversed array can then be joined back into a string using the implode() function.
$str = 'Hello';
$array = str_split($str);
$reversed_array = array_reverse($array);
$reversed_string = implode('', $reversed_array);
echo $reversed_string; // Output: 'olleH'
Code Explanation
The array_reverse() function returns a new array with its elements in reverse order.
Code Explanation
The implode() function joins the elements of an array into a single string, effectively converting the array into a string format.
Data Masking for Sensitive Information
To protect sensitive data by masking specific parts of a string, you can decompose the string into an array using the str_split() function and then modify the target elements.
$data = '123-45-6789'; // Example: Social Security Number (SSN)
$array = str_split($data);
$array_length = count($array);
// Mask characters starting from the 4th index with '*'
for ($i = 4; $i < $array_length; $i++) {
// Skip hyphen for a more natural masked look
if ($array[$i] !== '-') {
$array[$i] = '*';
}
}
$masked_data = implode('', $array);
echo $masked_data; // Output: '123-**-****'
Code Explanation
The for loop is a control structure used to execute a block of code for a specified number of iterations or within a defined range.
The examples above demonstrate how the str_split() function simplifies string manipulation. By partitioning a string into substrings, this function provides the flexibility needed to process and transform string data in a variety of ways.