Split string

radofeya

New Member
Messages
49
Reaction score
0
Points
0
Can I split a string word by word into an array by using PHP?

example:

$string = "hello";
then, split string into "h", "e", "l", "l", "o".

thanks
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
There's a function just for that :)
PHP:
$string = 'Hello there!';
$array = str_split($string);
print_r($array);
The above will output:
Code:
Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => t
    [7] => h
    [8] => e
    [9] => r
    [10] => e
    [11] => !
)
So the function you need is str_split. You can also choose to split the string every 2 letters, 3 letters, or whatever you desire, but the default will split every letter into it's own array key.
View PHP.net for more info.
 

scopey

New Member
Messages
62
Reaction score
0
Points
0
Similarly, you can just use explode() if you split by null:

PHP:
// returns an array with each character:
explode("Hello there!", "");
 
Top