Skip to content

Instantly share code, notes, and snippets.

@flaviosilveira
Created June 13, 2020 17:54
Show Gist options
  • Save flaviosilveira/c956f22c7ef674f0bfa1d8dc3eb823c4 to your computer and use it in GitHub Desktop.
Save flaviosilveira/c956f22c7ef674f0bfa1d8dc3eb823c4 to your computer and use it in GitHub Desktop.
<?php
$dir = '/Users/flaviosilveira/Dropbox/php-mentors/manuscript';
$result = array();
foreach(scandir($dir) as $file){
switch ($file){
case '.':
case '..':
case 'Book.txt':
case 'before_start.txt':
case 'disclaimer.txt':
case 'images':
case 'introduction.txt':
case 'profiles.txt':
case 'questions.txt':
case 'some_last_words.txt':
case 'what_are_they_talking_about.txt':
continue 2;
break;
default:
$stringArr = file($dir . '/' . $file);
$fileString = '';
foreach($stringArr as $string){
if(strpos($string, '---') === 0){
continue;
}
if(strpos($string, '**') === 0){
} else {
$fileString .= ' ' . $string . ' ';
}
}
$words = preg_split('/[\s]+/', $fileString, -1, PREG_SPLIT_NO_EMPTY);
$wordInFile = array();
foreach($words as $word){
$word = preg_replace("/[^a-zA-Z]/", "", $word);
$word = strtolower($word);
if(!in_array($word, $wordInFile)){
if(array_key_exists($word,$result)){
$result[$word] = $result[$word]+1;
} else {
$result[$word] = 1;
}
}
$wordInFile[] = $word;
}
}
}
arsort($result);
print_r($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment