Skip to content

Instantly share code, notes, and snippets.

@PaulFr
Created November 12, 2012 10:22
Show Gist options
  • Save PaulFr/4058520 to your computer and use it in GitHub Desktop.
Save PaulFr/4058520 to your computer and use it in GitHub Desktop.
statistiques d'un texte
<?php
$text = file_get_contents('ok.txt');
$words = explode(' ', $text);
$top = 50;
echo 'Il y a '.count($words)." mots\n";
echo 'Il y a '.count(array_unique($words))." mots uniques\n";
echo 'Il y a '.(strlen($text)).' caractères (en comptant les espaces)'."\n";
echo 'Il y a '.(strlen(str_replace(' ', '',$text))).' caractères (sans compter les espaces)'."\n";
$count = array();
foreach($words as $word){
if(!isset($count[$word])) $count[$word] = 0;
$count[$word]++;
}
arsort($count);
$i = 0;
if($top < count($count)) $top = count($count);
foreach($count as $word => $nb){
if(++$i > $top) break;
echo $i.'. '.utf8_encode($word).' -> '.$nb."\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment