I'm trying to build a class that will parse CSV files in a certain way. Unfortunately I am NOT an OO programmer in any way shape or form, my company is requiring me to write a class for some future functionality and I seriously, seriously need some help on it. So...
Our $value will be formulated by counting the second marker in the file which is a semicolon, and create a new feed for each carriage return.
Input is like this:
Code:
Jeff Goes, Mika Enrar;Triple Threat, Dogs on Bikes
Sonny Ray, Lars McGarvitch, Jason McKinley;Kasabian, Lords of Acid, Hard-Fi
So for ease of understandability, lets say that the names are $name, the bands are $item, and the equatible score is $value.
The Run Down: If the 开发者_开发问答number of letters in the $item is even (Triple Threat = 12) then the $value is equal to the number of vowels in $name times one and a half (Jeff Goes [3x1.5] = 4.50)
If the number of the $item is odd (Dogs on Bikes = 11) then the $value is the number of consonants in $name (Mika Enrar = 5)
If the number of letters in $name share a commonality in $item besides one, multiply the output by one and a half. So (Sonny Ray = 8; Kasabian = 8; then 6x1.5 = 9; or lets say $name = 112; $item = 12; we share a commonality of 12)
The idea is to implement a class that assigns each $name a $item to be offered in a way that maximizes the combined total $value across all of the $item. As a fail safe, there may be a different number of $name and $item.
My output should be the max $value in two decimal places as in 12.00 38.50
UPDATED ANSER
If you are looking for something a bit more OOP, and to do so with some built-in function of PHP, I would start by doing this:
class CSVParser
{
public $output = NULL;
public $digits = NULL;
public function __construct($file)
{
if (!file_exists($file)) {
throw new Exception("$file does not exist");
}
$this->contents = file_get_contents($file);
$this->output = array();
$this->digits = array();
}
public function parse($separatorChar1 = ',', $separatorChar2 = ';', $enclosureChar = '"', $newlineChar = "\n")
{
$lines = explode($newlineChar, $this->contents);
foreach ($lines as $line) {
if (strlen($line) == 0) continue;
$group = array();
list($part1, $part2) = explode($separatorChar2, $line);
$group[] = array_map(array($this, "trim_value"), explode($separatorChar1, $part1), array("$enclosureChar \t"));
$group[] = array_map(array($this, "trim_value"), explode($separatorChar1, $part2), array("$enclosureChar \t"));
$this->output[] = $group;
}
}
private function trim_value($value, $chars)
{
return preg_replace("#^( |" . $chars . ")+#", '', $value);
}
public function algorithm()
{
$alpha = array(
'c' => str_split('bcdfghjklmnpqrstvwxz'),
'v' => str_split('aeiouy')
);
$i = 0;
$k = 0;
foreach ($this->output as $item) {
$cnt = 0;
$this->digits[$i] = array();
foreach ($item as $part) {
$this->digits[$i][$cnt] = array();
$new = array();
foreach ($part as $str) {
$v = count(array_intersect(str_split($str), $alpha['v']));
$c = count(array_intersect(str_split($str), $alpha['c']));
$t = strlen(str_replace(' ', '', $str));
$new = array('v' => $v, 'c' => $c, 't' => $t);
$this->digits[$i][$cnt][] = $new;
}
$cnt++;
}
$i++;
}
}
}
$parser = new CSVParser("file.txt");
$parser->parse();
print_r($parser->output);
$parser->algorithm();
print_r($parser->digits);
精彩评论