Created
September 2, 2011 19:09
-
-
Save xwero/1189545 to your computer and use it in GitHub Desktop.
unique word count from text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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