All pastes #147114 Raw Edit

mash

public text v1 · immutable
#147114 ·published 2006-08-24 03:58 UTC
rendered paste body
<?php
error_reporting(E_ALL);
$text = 'some text to do something with, and some more text';

// empty array of words
$words = array(); 
// split the text the spaces
$hash = explode(' ',$text);

// loop the array
foreach($hash as $word){
	// get rind of spaces (incase there was three spaces
	$w = trim($word);

	// check if the $w is in the array
	if(array_key_exists($w,$words)){
		$words[$w]++; //increment the count
	}else{
		$words[$w] = 1;
	}
	
}
foreach($words as $word => $count){
	echo "$word = $count<br>";
}