PDA

View Full Version : [SOLVED] php split string function, can collect into an array?



sdowney717
January 16th, 2011, 09:00 PM
this will split a string into words using the space as the pattern
and collects 6 words.
but how would you do this with an array and catch however many words exist?



list($andws1,$andws2,$andws3,$andws4,$andws5,$andw s6) = split(' ', $andws);
echo $andws1;
echo $andws2;
echo $andws3;
echo $andws4;
echo $andws5;
echo $andws6;

sdowney717
January 16th, 2011, 09:16 PM
Ok, I got it with this


$s = "Split this sentence by spaces";
$words = split(' ', $s);
print count($words);
echo $words[0];
echo $words[1];
echo $words[2];

laceration
January 17th, 2011, 03:21 AM
foreach ( $words as $key=>$val )
{
echo $words[$key];
}