?
Solved

scan a textfile

Posted on 2011-03-10
8
Medium Priority
?
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Assisted Solution

by:Aaron Tomosky
Aaron Tomosky earned 1000 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

Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

762 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