Solved

scan a textfile

Posted on 2011-03-10
8
350 Views
Last Modified: 2012-05-11
want to scan a text file for everyline that starts like this an echo it
03/10 11:16:05AM | q1 | INSERT INTO payments


datetime | q1 | INSERT INTO payments
0
Comment
Question by:rgb192
  • 4
  • 2
  • 2
8 Comments
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 35096682
Filegetcontents will load the file
Explode on \r\l or whatever your new line is
Foreach line in your new array of lines look for whatever ad echo it if it's there.
0
 

Author Comment

by:rgb192
ID: 35097235
I dont understand what the regular expression would be
0
 
LVL 4

Accepted Solution

by:
Doug Walton earned 250 total points
ID: 35102022
Maybe something kind of like this:
$fp = fopen("FILEPATH", "r");

while ($line = fgets($fp))
{
  $first = trim(strtok($line, "|"));
  if(strtotime($first) != false)  //see if it's a date
  {
    $second = trim(strtok("|"));
    if($second == "q1")  //make sure it says q1
    {
        $third = trim(strtok("|"));
        if(strncmp($third, "INSERT INTO payments", 20) == 0) //if it's an insert, print it.
          echo $line;
    }
  }
}

Open in new window



Does that help/make sense?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 39

Assisted Solution

by:Aaron Tomosky
Aaron Tomosky earned 250 total points
ID: 35102196
to clarify my previous answer:
$file = filegetcontents('filepath');
$lines = explode($file,'\r\n');
foreach($lines as $line)
{
//do the regex or what doug put here
}

0
 

Author Comment

by:rgb192
ID: 35217476
$fp = fopen("FILEPATH", "r");

while ($line = fgets($fp))
{
  $first = trim(strtok($line, "|"));
  if(strtotime($first) != false)  //see if it's a date
  {
    $second = trim(strtok("|"));
    if($second == "q1")  //make sure it says q1
    {
        $third = trim(strtok("|"));
        if(strncmp($third, "INSERT INTO payments", 20) == 0) //if it's an insert, print it.
          echo $line;
    }
  }
}

how to make this end at |  (the fourth |)
0
 

Author Comment

by:rgb192
ID: 35217480
i meant the third |
0
 

Author Closing Comment

by:rgb192
ID: 35298352
both code samples answer the question.  I need to re ask question to find out what is after the third |
0
 
LVL 4

Expert Comment

by:Doug Walton
ID: 35316060
If you just do another strtok("|") that should get what's after the third |.  Does that help?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question