5

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..?

j0k
  • 22,600
  • 28
  • 79
  • 90
AnNaMaLaI
  • 4,064
  • 11
  • 53
  • 93
  • 2
    in php stings can be addressed as an array –  Sep 04 '12 at 10:08
  • 1
    Might I ask why? `$str = 'overflow'; echo $str[0];` echoes 'o' as it is. You can treat any string like an array without splitting it first anyhow – Elias Van Ootegem Sep 04 '12 at 10:09
  • @Wooble Even though this can be easily found on google, it would still be best if the amount of questions on SO would grow, with more interesting questions with every day. – Lucas Sep 04 '12 at 10:11
  • @Elias Van Ootegem : I need a function.. because i need to separate lot of string this way.. so only.. otherwise i can find the string length and upto that string $str[n].. like that i can use by your way.. – AnNaMaLaI Sep 04 '12 at 10:12
  • 3
    @think123 do you really like to answer the same questions over and over again and try to find something more interesting in a flood of the same, elementary, gazzilion-times-answered questions? this increases noise/signal ration in the bad direction. – Marcin Orlowski Sep 04 '12 at 10:13
  • 1
    possible duplicate of [Convert a String into an Array of Characters](http://stackoverflow.com/questions/2768314/convert-a-string-into-an-array-of-characters) – Wooble Sep 04 '12 at 10:15
  • @Wooble yes, but if the question is not already on Stack Overflow, it would be good to populate the Stack Overflow database in a positive way. – Lucas Sep 04 '12 at 10:15
  • @Wooble: I searched in stackoverflow but i could not found any solution so only i posted.. I not posted blindly :( – AnNaMaLaI Sep 04 '12 at 10:20
  • Does this answer your question? [Convert a String into an Array of Characters](https://stackoverflow.com/questions/2768314/convert-a-string-into-an-array-of-characters) – ggorlen Nov 18 '22 at 14:34

7 Answers7

6

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);
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
5

use this function --- str_split();

This will split the string into character array.

Example:

$word="overflow";
$split_word=str_split($word);
Santhiya
  • 351
  • 3
  • 12
3

Try like this....

$var = "OVERFLOW";
echo $var[0]; // Will print "O".
echo $var[1]; // Will print "V".
Vins
  • 8,849
  • 4
  • 33
  • 53
1

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

Baba
  • 94,024
  • 28
  • 166
  • 217
1

You could do:

$string = "your string";
$charArray = str_split($string);
Prashant
  • 152
  • 1
  • 16
1

Take a look at str_split

You can use it like this:

array str_split ( string $string [, int $split_length = 1 ] );
Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
chandan
  • 123
  • 13
0

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.

Lucas
  • 16,930
  • 31
  • 110
  • 182