Skip to content

Instantly share code, notes, and snippets.

@salayhin
Created August 20, 2013 23:08
Show Gist options
  • Save salayhin/6288494 to your computer and use it in GitHub Desktop.
Save salayhin/6288494 to your computer and use it in GitHub Desktop.
<?php
/**
* Author Md. Sirajus Salayhin <salayhin@gmail.com>
* 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