首頁 繪圖設計 工作閒聊 比賽活動 美術討論 標籤 圖片
PHP cli for summing up Uber Taxi Fare from Amount Mail eml files (2026/05)
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)
[更多討論] 討論區 Windows, Linux, Perl, PHP, C/C++, Driver, Web 理論、應用、硬體、軟體

"PHP cli for summing up Uber Taxi Fare from Amount Mail eml files (2026/05)" 傳統頁面(電腦版)

首頁 繪圖設計 工作閒聊 比賽活動 美術討論 標籤 圖片
傳統桌面版 [ 登入/註冊 ]
© Vovo2000.com Mobile Version 小哈手機版 2026