Skip to content

Instantly share code, notes, and snippets.

@splitbrain
Forked from isotopp/apps.php
Last active December 31, 2015 03:49

Revisions

  1. splitbrain revised this gist Dec 12, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions apps.php
    Original file line number Diff line number Diff line change
    @@ -92,5 +92,4 @@ function __construct($c) {
    $currency[$v->currency]['title'] .= $v->price >0?"{$v->name} for {$v->price}\n":"";
    }

    var_dump($currency);
    ?>
    print_r($currency);
  2. @isotopp isotopp created this gist Dec 12, 2013.
    96 changes: 96 additions & 0 deletions apps.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    #! /usr/bin/php
    <?php

    class AndroidApp {
    #0: Trees for Cars by Leo Grand
    #1: 0,99 $
    #2: December 11, 2013
    #3: Complete
    #4: Android Apps
    #5: Transportation
    var $name;
    var $price;
    var $currency;
    var $date;
    var $status;
    var $type;
    var $category;

    function fix_currency() {
    $curr = $this->price;

    if (strpos($curr, "\$") !== FALSE) {
    $this->currency = "USD";
    $this->price = trim(substr($curr, 0, -1));
    }

    if (strpos($curr, "") !== FALSE) {
    $this->currency = "EUR";
    $this->price = trim(substr($curr, 0, -3));
    }

    if (strpos($curr, "£") !== FALSE) {
    $this->currency = "UKP";
    $this->price = trim(substr($curr, 0, -3));
    }

    if (strpos($curr, "CHF") !== FALSE) {
    $this->currency = "CHF";
    $this->price = trim(substr($curr, 0, -3));
    }
    if ($curr === "Free") {
    $this->currency = "EUR";
    $this->price = "0.00";
    }

    $this->price = str_replace(",", ".", $this->price);
    $this->price += 0.00;
    return;
    }

    function __construct($c) {
    $a = array();

    for ($i=0; $i<6; $i++) {
    if ($c->eof())
    return;

    $a[] = trim($c->fgets());
    }

    $this->name = $a[0];
    $this->price = $a[1];
    $this->date = $a[2];
    $this->status = $a[3];
    $this->type = $a[4];
    $this->category = $a[5];

    $this->fix_currency();

    return $this;
    }
    }


    try {
    $file = new SplFileObject("apps.txt");
    } catch (RuntimeException $e) {
    die($e->getMessage(). "\n");
    };
    $file->setFlags(SplFileObject::DROP_NEW_LINE);

    $apps = array();
    while (!$file->eof()) {
    $a = new AndroidApp($file);
    if (!$file->eof())
    $apps[] = $a;
    }

    foreach ($apps as $k => $v) {
    $currency[$v->currency]['sum'] += $v->price;
    $currency[$v->currency]['count'] ++;
    $currency[$v->currency]['title'] .= $v->price >0?"{$v->name} for {$v->price}\n":"";
    }

    var_dump($currency);
    ?>