PHP str_split() Function
In this chapter you will learn:
- Definition for PHP str_split() Function
- Syntax for PHP str_split() Function
- Parameter for PHP str_split() Function
- Return for PHP str_split() Function
- Example - Split the string "Hello" into an array
- Example - Set the length of each array element
Definition
The str_split() function splits a string into an array.
Syntax
PHP str_split() Function has the following syntax.
str_split(string,length)
Parameter
Parameter | Is Required | Description |
---|---|---|
string | Required. | String to split |
length | Optional. | Length of each array element. Default is 1 |
Return
If length is less than 1, the str_split() function will return FALSE.
If length is larger than the length of string, the entire string is returned as the only element in the array.
Example 1
Split the string "Hello" into an array:
<?php
print_r(str_split("Hello"));
?>
The code above generates the following result.
Example 2
Using the length parameter:
<?php
print_r(str_split("Hello from java2s.com",3));
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP str_word_count() Function
- Syntax for PHP str_word_count() Function
- Parameter for PHP str_word_count() Function
- Format for PHP str_word_count() Function
- Return for PHP str_word_count() Function
- Example - Word count
Home » PHP Tutorial » PHP String Functions