Type(Type) 2026/5/31 17:33
php.exe <taxi_uber_sum_fare.php> <eml_files_folders>
代碼:
<?php
declare(strict_types=1);
if ($argc < 2) {
fwrite(STDERR, "Usage: php taxi_uber_sum_fare.php <eml-directory>\n");
exit(1);
}
$dir = $argv[1];
if (!is_dir($dir)) {
fwrite(STDERR, "Not a directory: {$dir}\n");
exit(1);
}
$files = glob(rtrim($dir, "\\/") . DIRECTORY_SEPARATOR . '*.eml');
sort($files, SORT_NATURAL | SORT_FLAG_CASE);
if (!$files) {
fwrite(STDERR, "No .eml files found in {$dir}\n");
exit(1);
}
function extractHtmlPart(string $raw): ?string
{
if (!preg_match('/Content-Type:\s*text\/html.*?\R\R(.*?)(?=\R--=|$)/si', $raw, $m)) {
return null;
}
return quoted_printable_decode($m[1]);
}
function extractAmount(string $html): ?array
{
if (!preg_match('/data-testid="total_fare_amount"[^>]*>([^<]+)/i', $html, $m)) {
return null;
}
$label = html_entity_decode(trim($m[1]), ENT_QUOTES | ENT_HTML5, 'UTF-8');
$label = preg_replace('/\s+/u', ' ', $label);
if (!preg_match('/([A-Z]{0,3}\$)\s*([0-9,]+(?:\.[0-9]{2})?)/u', $label, $n)) {
if (!preg_match('/([0-9,]+(?:\.[0-9]{2})?)/u', $label, $n)) {
return null;
}
$currency = '';
$number = $n[1];
} else {
$currency = $n[1];
$number = $n[2];
}
$number = str_replace(',', '', $number);
$cents = (int)round(((float)$number) * 100);
return [
'raw' => $label,
'currency' => $currency,
'number' => $number,
'cents' => $cents,
];
}
$rows = [];
$totalCents = 0;
$failures = [];
foreach ($files as $file) {
$raw = file_get_contents($file);
if ($raw === false) {
$failures[] = basename($file) . " (read error)";
continue;
}
$html = extractHtmlPart($raw);
if ($html === null) {
$failures[] = basename($file) . " (html not found)";
continue;
}
$amount = extractAmount($html);
if ($amount === null) {
$failures[] = basename($file) . " (amount not found)";
continue;
}
$totalCents += $amount['cents'];
$rows[] = [
'file' => basename($file),
'amount' => $amount['currency'] . number_format($amount['cents'] / 100, 2, '.', ''),
];
}
foreach ($rows as $row) {
echo $row['file'] . "\t" . $row['amount'] . PHP_EOL;
}
echo str_repeat('-', 40) . PHP_EOL;
echo 'Count: ' . count($rows) . PHP_EOL;
echo 'Total: ' . number_format($totalCents / 100, 2, '.', '') . PHP_EOL;
if ($failures) {
echo str_repeat('-', 40) . PHP_EOL;
echo "Failures:" . PHP_EOL;
foreach ($failures as $failure) {
echo $failure . PHP_EOL;
}
exit(2);
}
(16 views)
© Vovo2000.com Mobile Version 小哈手機版 2026