Skip to content

Instantly share code, notes, and snippets.

@xwero
Created September 2, 2011 19:09
Show Gist options
  • Save xwero/1189545 to your computer and use it in GitHub Desktop.
Save xwero/1189545 to your computer and use it in GitHub Desktop.
unique word count from text
<?php
/**
* transfrom text to array with unique words and their appearance count
*
* @param string text
* @param array excludes
* @param string sort prefix php sort function, by default alphabetic
* @return array the words as keys and the appearance count as value
*/
function word_count($text, $excludes=array(), $sort = 'k')
{
$cloud = array();
$words = str_word_count($text, 1);
foreach($words as $word)
{
if( ! in_array($word,$excludes))
{
$cloud[$word] = substr_count($text, $word);
}
}
$sort .= 'sort';
$sort($cloud);
return $cloud;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment