Solved

scan a textfile

Posted on 2011-03-10
8
348 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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 …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

777 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