Pulling date-specific data from flat file

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm almost a complete newbie to using PHP and I was hoping one of you
guys can point me in the right direction. I'm working on a site for a
brand of handmade fruit juice and it only needs one bit of dynamic
content: the info on what fruit is in the juice and where it is coming
from (vendor, country etc.) today and tomorrow (the content of the
juice is different everyday). So I thought it would be easiest to put
that information in a flat file (an Excel file would be ideal but a
plain text would do too) on a weekly basis and try to pull out the
needed info with PHP. Except I don't know how yet. I don't expect any
of you to do the work for me but if someone could point me to which
functions of PHP I should do some research on, I'd be most grateful.

Kind regards,

Daniel Baars

Re: Pulling date-specific data from flat file

I have a PHP routine that does an RSS website scraping process only
once per day - I use an external file to trigger whether it does
anything or not.  Here's what I use:

  $inData   = file_get_contents('/xml/ts/data1.txt');
  $str      = file_put_contents('/xml/ts/data1.txt', $pubDate);
  if ($inData != $pubDate) { ...do this process ... }

The first statement retrieves a publication date that was last saved.
The second statement writes the new publication date to the file.
Then, I only do my daily process if the publication date I retrieved is
different from the one I just wrote.  Technically, I probably should
only write the new date AFTER my process is complete -- just in case a
problem occurs and the process doesn't complete.

But, anyway, those two functions are a nice easy way to read and write
a flat file.

Quoted text here. Click to load it

Re: Pulling date-specific data from flat file

Let's say your target page, is this pretty decent sized document at
W3.org: http://validator.w3.org/docs/users.html

View the page source manually to find some unique portions that
delineate the important section, and plug them into the $cStartStr and
$cEndStr variables. For this exercise I chose to extract the section
labeled "Interpreting the results".

    $cTargetPage = "http://validator.w3.org/docs/users.html ";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $cTargetPage);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

    $cPgStr = curl_exec($ch);

    $cStartStr = "<div id=\"Interpret\" class=\"stb\">";
    $cEndStr = "</div>";
    $cPageTail = stristr($cPgStr, $cStartStr);
    $nUsefulDataEndPos = strpos($cPageTail, $cEndStr);
    $cUsefulData = substr($cPageTail, 0, $nUsefulDataEndPos+6);

Run the script and save the relevent portion somewhere useful instead
of echoing it out. :-)

Re: Pulling date-specific data from flat file

write your data as csv and shell out to grep

that will provide you with the ability to import/export your flat file  
from excel and to perform quite complex lookups without any difficult
code to write.

its what grep is for.


Re: Pulling date-specific data from flat file

danielbaars wrote:

Quoted text here. Click to load it

If you create a text file much like this:

    20061103,Pineapple,the Azores

You should be able to pull that information into PHP very easily using the
file() function; find out today's date using date(); then loop through
each line of the file comparing the first few characters of the file with
today's date, something like this:

    $today = date('Ymd');
    $lines = file('data.txt');
    $info = FALSE;
    while (!$info)
        $thisline = array_pop($lines);
        if ($today==substr($thisline,0,8))
            $info = $thisline;

This will find you the relevent line and store it into a variable called

The rest is easy-peasy...

    list($today,$fruit,$location) = explode(',', $info);
    printf("Today's drink is %s juice from %s.", $fruit, $location);


Toby A Inkster BSc (Hons) ARCS
Contact Me  ~ http://tobyinkster.co.uk/contact

Re: Pulling date-specific data from flat file

Wow! Thank you all so much! The last example is very close to what I'm
trying to do. I got this to work no problem but how do I list more than
one item and also the items for tomorrow?

The text file would be something like:
        20061101,Apple,Normandy,John Smith
        20061101,Bananas,Chili,Jack Jones
        20061101,Orange,Seville,Robert Brown
        20061101,Coconut,Fiji,Ed Johnson
        20061102,Apple,California,Tess Williams
        20061102,Bananas,Argentina,Joe Taylor
        20061102,Orange,South Africa,Sarah Anderson
        20061102,Coconut,Hawaii,Harry Thompson

And make it into a listing like:
The apples in our juice comes from:
Today: Normandy (by John Smith)
Tomorrow: California (by Tess Williams)

The bananas in our juice comes from:
Today: Chili (by Jack Jones)
Tomorrow: Argentina (by Joe Taylor)


Many thanks,


Re: Pulling date-specific data from flat file

danielbaars@gmail.com says...
Quoted text here. Click to load it

like I said - using grep makes life easier.


Re: Pulling date-specific data from flat file

Thanks for your reply. Could you elaborate a bit? I've been
investigating the PHP grep combination for the past few days but the
few bits I could find on it I found very hard to apply to my needs. The
regular expressions are not the biggest problem, I've worked with them
before and I'm reasonably sure I can figure out how to select the right
data in the CSV file. What I find hard to understand (and hard to find
info on) is how to make it interact with PHP. For instance, how do I
put the right date for 'today' and 'tomorrow' in my regular expression?
And how do I use the data that the regex has found?


On Nov 1, 6:23 pm, nige...@by.uk wrote:
Quoted text here. Click to load it

Site Timeline