Solved

scan a textfile

Posted on 2011-03-10
8
343 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
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 count occurrences of each item in an array.

706 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

22 Experts available now in Live!

Get 1:1 Help Now