In PHP is there any function to break up string in to characters or array.
Example: OVERFLOW
I need to break up the above text OVERFLOW int to: O V E R F L O W
OR
array(
0=> 'O',
1=> 'V',
2=> 'E',
3=> 'R',
4=> 'F',
5=> 'L',
6=> 'O',
7=> 'W'
)
or any otherway is there..?
The str_split() is an inbuilt function in PHP and is used to convert the given string into an array. This function basically splits the given string into smaller strings of length specified by the user and stores them in an array and returns the array.
explode() is a built in function in PHP used to split a string in different strings. The explode() function splits a string based on a string delimiter, i.e. it splits the string wherever the delimiter character occurs. This functions returns an array containing the strings formed by splitting the original string.
PHP - Function split() The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string.
The explode() function breaks a string into an array. Note: The "separator" parameter cannot be an empty string. Note: This function is binary-safe.
There is a function for this: str_split
$broken = str_split("OVERFLOW", 1);
If your string can contain multi byte characters, use preg_split
instead:
$broken = preg_split('##u', 'OVERFLOW', -1, PREG_SPLIT_NO_EMPTY);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With