Last active
August 29, 2015 14:06
-
-
Save 0xff00ff/65182becb7314978c0bc 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 | |
$basePrices = [ | |
"6,00 € /l" => ['price' => '6,00', 'volume' => '1', 'unit' => 'l'], | |
"1.88 € / 100 g" => ['price' => '1.88', 'volume' => '100', 'unit' => 'g'], | |
"1.15 € / 100 gr" => ['price' => '1.15', 'volume' => '100', 'unit' => 'gr'], | |
"2.33 € / 1 kg" => ['price' => '2.33', 'volume' => '1', 'unit' => 'kg'], | |
"2.33 € / kg" => ['price' => '2.33', 'volume' => '1', 'unit' => 'kg'], | |
"0,70 € / 10St" => ['price' => '0,70', 'volume' => '10', 'unit' => 'St'], | |
"2.49 € / Stück" => ['price' => '2.49', 'volume' => '1', 'unit' => 'Stück'], | |
"8.6 € / 100 Gramm" => ['price' => '8.6', 'volume' => '100', 'unit' => 'Gramm'], | |
"6,00 € /l" => ['price' => '6,00', 'volume' => '1', 'unit' => 'l'], | |
"22.21 EUR / Liter" => ['price' => '22.21', 'volume' => '1', 'unit' => 'Liter'], | |
"10,53 € / 1.000ml" => ['price' => '10,53', 'volume' => '1.000', 'unit' => 'ml'], | |
"8,73 € / 1.000l" => ['price' => '8,73', 'volume' => '1.000', 'unit' => 'l'], | |
"13,20 €" => ['price' => '13,20', 'volume' => '', 'unit' => ''], | |
"91,33 €/L" => ['price' => '91,33', 'volume' => '1', 'unit' => 'L'], | |
"6.39€/1l" => ['price' => '6.39', 'volume' => '1', 'unit' => 'l'], | |
"15,33 € / 1l" => ['price' => '15,33', 'volume' => '1', 'unit' => 'l'], | |
]; | |
function getBasePrice($str) { | |
$result = []; | |
if (preg_match("/(.*)\/(.*)/", $str, $matches)) { | |
if (preg_match("/([\d,\.]+)/", $matches[1], $priceMatches)) { | |
$result['price'] = trim($priceMatches[1]); | |
} | |
if (preg_match("/([\d,\.]+)\s*(.*)/", $matches[2], $volumeMatches)) { | |
$result['volume'] = trim($volumeMatches[1]); | |
$result['unit'] = trim($volumeMatches[2]); | |
} else { | |
$result['volume'] = '1'; | |
$result['unit'] = trim($matches[2]); | |
} | |
} else { | |
if (preg_match("/([\d,\.]+)/", $str, $priceMatches)) { | |
$result['price'] = trim($priceMatches[1]); | |
$result['volume'] = ''; | |
$result['unit'] = ''; | |
} | |
} | |
return $result; | |
} | |
foreach ($basePrices as $basePriceStr => $result) { | |
$basePrice = getBasePrice($basePriceStr); | |
if (count($result) === 3 | |
&& $basePrice['price'] === $result['price'] | |
&& $basePrice['volume'] === $result['volume'] | |
&& $basePrice['unit'] === $result['unit']) { | |
echo chr(27) . "[42m", " OK "; | |
} else { | |
echo chr(27) . "[41m", "FAIL"; | |
} | |
echo chr(27) . "[0m"; | |
echo " ", $basePriceStr . "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment