Created
August 20, 2013 23:08
-
-
Save salayhin/6288494 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Author Md. Sirajus Salayhin <[email protected]> | |
* Class for parsing number | |
*/ | |
class Parser | |
{ | |
private $numbers = array(); | |
private $oddNumbers = array(); | |
public function __construct($filePath) | |
{ | |
$fp = fopen($filePath, 'r'); | |
while(($str = fgets($fp))) { | |
preg_match_all('/([0-9]+)/', $str, $matches); | |
if (!empty($matches[0])) { | |
foreach ($matches[0] AS $number) { | |
$this->numbers[] = $number; | |
if ($number % 2) { | |
$this->oddNumbers[] = $number; | |
} | |
} | |
} | |
} | |
} | |
public function addNumber() | |
{ | |
return array_sum($this->numbers); | |
} | |
public function getOddNumbers() | |
{ | |
return $this->oddNumbers; | |
} | |
public function getAllNumbers() | |
{ | |
return $this->numbers; | |
} | |
} | |
// Create object | |
$parser = new Parser('test.txt'); | |
$parser->addNumber(); | |
$parser->getOddNumbers(); | |
$parser->getAllNumbers(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment