Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4258872
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:42:26+00:00 2026-05-21T05:42:26+00:00

I need an advice. I need to scrap and parse text file (using for

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:42:27+00:00Added an answer on May 21, 2026 at 5:42 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.