Jump to content

How to split a string into words


hno

Recommended Posts

Or if you're a kind of DIY person:

 

$string = 'aaa bbb ccc';

$words = array();

for ($i = 0, $j = 0, $length = strlen($string); $i < $length; ++$i) {
if ($string[$i] == ' ') {
	$j++;
	continue;
}

if (!isset($words[$j])) {
	$words[$j] = $string[$i];
}
else {
	$words[$j] .= $string[$i];
}
}

var_dump($words);

 

Just for fun and because I had a little extra time this morning - use explode() instead :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.