PHP: str_split() function
Description
The str_split() function is used to convert a string to an array.
Version:
(PHP 5 )
Syntax:
str_split(string_name, split_length)
Parameters:
Name | Description | Required / Optional |
Type |
---|---|---|---|
string_name | The string to be split. | Required | String |
split_length | The length of the chunk. | Optional | Integer |
Return value:
If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise, each chunk will be one character in length.
FALSE is returned if split_length is less than 1.
If the split_length length exceeds the length of the string, the entire string is returned as the first (and only) array element.
Value Type: Array.
Pictorial Presentation
Example:
<?php
$string_name='Welcome to w3resource.com';
print_r(str_split($string_name));
echo '<br>';
print_r(str_split($string_name,4));
echo '<br>';
?>
Output:
Array ( [0] => W [1] => e [2] => l [3] => c [4] => o [5] => m [6] => e [7] => [8] => t [9] => o [10] => [11] => w [12] => 3 [13] => r [14] => e [15] => s [16] => o [17] => u [18] => r [19] => c [20] => e [21] => . [22] => c [23] => o [24] => m ) Array ( [0] => Welc [1] => ome [2] => to w [3] => 3res [4] => ourc [5] => e.co [6] => m )
View the example in the browser
See also
Previous: str_shuffle
Next: str_word_count
PHP: Tips of the Day
PHP: Remove first 4 characters of a string with PHP
You could use the substr function to return a substring starting from the 5th character:
$str = "The quick brown fox jumps over the lazy dog." $str2 = substr($str, 4); // "quick brown fox jumps over the lazy dog."
Ref : https://bit.ly/2LJDLQx
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework