Skip to content

Instantly share code, notes, and snippets.

@mavimo
Created March 31, 2016 14:21
Show Gist options
  • Save mavimo/dd35e6e73a30075e1812571d229e4fbb to your computer and use it in GitHub Desktop.
Save mavimo/dd35e6e73a30075e1812571d229e4fbb to your computer and use it in GitHub Desktop.
<?php
class FileContentCounter
{
private $file;
public function __construct($file)
{
$this->file = $file;
}
public function getWordsCount()
{
$text = $this->getFileContent();
return str_word_count($text);
}
public function getSentenceCount()
{
$text = $this->getFileContent();
$num_frasi = count(explode(".\n", $text));
$num_frasi += count(explode("!\n", $text));
$num_frasi += count(explode("?\n", $text));
return $num_frasi;
}
private function getFileContent()
{
if (file_exists($this->file)) {
$text = file_get_contents($this->file);
} else {
$text = '';
}
return $text;
}
}
$counter = new FileContentCounter('test.txt');
print " - Parole : " . $counter->getWordsCount();
print " - Frasi : " . $counter->getSentenceCount();
file_put_contents('test.txt', 'contenuto');
print " - Parole : " . $counter->getWordsCount();
print " - Frasi : " . $counter->getSentenceCount();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment