简体   繁体   中英

Parsing Text File

I need an advice.

I need to scrap and parse text file (using for currency exchange rates). Here we are, a small snippet from this file:

c057z110323
h057z110323
a057z110323
b012z110323
c058z110324
h058z110324
a058z110324
c059z110325
h059z110325
a059z110325
c060z110328
h060z110328
a060z110328
c061z110329
h061z110329
a061z110329
c062z110330
h062z110330
a062z110330
b013z110330
c063z110331
h063z110331
a063z110331
c064z110401
h064z110401
a064z110401
c065z110404
h065z110404
a065z110404
c066z110405
h066z110405
a066z110405
c067z110406
h067z110406
a067z110406
b014z110406
c068z110407
h068z110407
a068z110407
c069z110408
h069z110408
a069z110408

As you may see there's a lot of lines (in original file there are about 80000 of lines (few lines per day are being added).

String format is as following:

A000112233  
where  
A - type  
000 - number of the file (created this year)  
11 - year  
22 - month  
33 - day 

I'm getting 25 latest lines from from the file using following snippet:

    $file = "http://www.nbp.pl/kursy/xml/dir.txt";
    $data = file($file);
    $count = count($data);

    for($i = $count - 25; $i < $count; $i++)
    {
        if( substr($data[$i], 0, 1) === 'a' )
        {
            $latest[] = $data[$i];
        }
    }

I need to get only lines starting with "a". The output array looks as following:

array(8) {
  [0]=>
  string(13) "a062z110330
"
  [1]=>
  string(13) "a063z110331
"
  [2]=>
  string(13) "a064z110401
"
  [3]=>
  string(13) "a065z110404
"
  [4]=>
  string(13) "a066z110405
"
  [5]=>
  string(13) "a067z110406
"
  [6]=>
  string(13) "a068z110407
"
  [7]=>
  string(13) "a069z110408
"
}

Now I need to compare every array element to get the latest item from the latest working day before current date. I'm acheiving it this way:

        $i = 1;
        foreach($latest as $row)
        {
            $plural = ($i > 1) ? 's' : null;

            if( substr(trim($row), -6) === date("ymd", strtotime("-" . $i . " day" . $plural) )
            {
                $filename = $row;
                break;
            }

            $i++;
        }

It's working quite OK, however I'm facing one big problem. I'm unable to sort $latest array by the latest six characters. I tried doing this using sort(), rsort(). None of them worked good for me.

Can anybody help me with this issue or maybe has better approach to do what I'm looking for.

You need to use a custom sort method. You can use usort to write your own comparison function : http://php.net/manual/en/function.usort.php

From the manual

function cmp($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

$a = array(3, 2, 5, 6, 1);

usort($a, "cmp");

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

When you do

for($i = $count - 25; $i < $count; $i++)
{
    if( substr($data[$i], 0, 1) === 'a' )
    {
        $latest[] = $data[$i];
    }
}

use date as a key in $latest array:

for($i = $count - 25; $i < $count; $i++)
{
    if( substr($data[$i], 0, 1) === 'a' )
    {
        $key = (int) substr($data[$i], -6);
        $latest[$key] = $data[$i];
    }
}

Then you can sort by key like:

ksort($latest);

Since you're only asking how to sort an array of strings by their last six characters:

Use usort :

function sortfunc($a, $b) {
  return strcmp(substr($a, -6), substr($b, -6));
}

usort($latest, 'sortfunc');

You may need to trim() your lines first or the newlines and/or carriage return characters will be part of the last 6 characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM