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..?
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);
use this function --- str_split();
This will split the string into character array.
Example:
$word="overflow";
$split_word=str_split($word);
Try like this....
$var = "OVERFLOW";
echo $var[0]; // Will print "O".
echo $var[1]; // Will print "V".
Use str_split
$str = "OVERFLOW" ;
$var = str_split($str, 1);
var_dump($var);
Output
array
0 => string 'O' (length=1)
1 => string 'V' (length=1)
2 => string 'E' (length=1)
3 => string 'R' (length=1)
4 => string 'F' (length=1)
5 => string 'L' (length=1)
6 => string 'O' (length=1)
7 => string 'W' (length=1)
Example
Tell you the truth, it already is broken up. So this would work:
$string = 'Hello I am the string.';
echo $string[0]; // 'H'
If you specifically want to split it, you could do this:
$string = 'Hello I am the string.';
$stringarr = str_split($string);
Depends if you really need to split it or not.
You could do:
$string = "your string";
$charArray = str_split($string);
Take a look at str_split
You can use it like this:
array str_split ( string $string [, int $split_length = 1 ] );