Solved

scan a textfile

Posted on 2011-03-10
8
345 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 38

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
 
LVL 38

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now