function extractWords($s) {
$stopWords = array('i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you',
'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her',
'hers', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves',
'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 'was',
'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing',
'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at',
'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before',
'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over',
'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why',
'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no',
'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can',
'will', 'just', 'don', 'should', 'now');
$s = strtolower($s);
$s = trim($s);
// remove all chars except a-zA-Z
$s = preg_replace('/[^a-zA-Z]/', ' ', $s);
// Remove multiple whitespace
$s = preg_replace('/\s\s+/i', ' ', $s);
preg_match_all('/\b.*?\b/i', $s, $words);
$words = $words[0];
// Get only words that are not stop words with length > 2
foreach ($words as $key=>$val) {
if ($val == '' || in_array(strtolower($val), $stopWords) || strlen($val) <= 2) {
unset($words[$key]);
}
}
// Remove duplicate words and count word occurrences
$thewords = array();
if (is_array($words)) {
foreach ($words as $key => $val) {
if (isset($thewords[$val]) ) {
$thewords[$val]++;
} else {
$thewords[$val] = 1;
}
}
}
arsort($thewords);
return $thewords;
}
$text = "PHP is a general-purpose 0 scripting language 1 especially suited
to web development. It was originally 123 created by Danish-Canadian
programmer Rasmus Lerdorf in 1994; the PHP reference
implementation 6 is now (produced) by The PHP Group.
PHP code is usually processed on a [web server]
by a PHP interpreterr";
$words = extractWords($text);
echo implode(', ', array_keys($words));
/*
run:
php, web, rasmus, server, processed, usually, code, group, produced, now, implementation,
reference, lerdorf, programmer, general, canadian, danish, created, originally, development,
suited, especially, language, scripting, purpose, interpreterr
*/