Created
March 31, 2016 14:21
-
-
Save mavimo/dd35e6e73a30075e1812571d229e4fbb to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| 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