Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading part of text file into array

Status
Not open for further replies.

jnyvtka

Programmer
Feb 27, 2002
5
US
Hello I have been struggling with this a bit so if anyone can offer any suggestions that would be great.

I am writing a php page that need to do the following:

1. Look in a certain directory for a new file that gets created every morning.
2. Grab the date and a number from a certain section of the file.
3. E-mail various people based on that number.
4. FTP that date/number to an appendable file on another server.

I can handle 1,3 and 4 I am stuck on the second part and my brain is not working at the moment.

Here is what I am looking at:

I have a text file that a new one gets created everyday and is named file01062005.txt inside that file there is a section that is in this format:

COUNT_BEGIN
01012005 1 3 4 1234
01022005 1 44 44 1234
01032005 1 123 44 1234
01042005 1 55 44 1234
COUNT_END

COUNT_BEGIN and COUNT_END will always be constant the information in between those tags will change.

I need to grab information from whatever the last line is such as in this case I need to grab

01042005
55

Then I would just take those Values and set my two variable AW_date and AW_count and then I can do my conditional work.

Any ideas how to do this in PHP or where to start.

Thanks-
 
What method do you use to read the text file?
Are all lines ended with carriage returns and/or line feeds?
If you read the file row by row just maintain the last read line in a variable so that when you read the line that says COUNT_END you have the line previous to it in the variable and can handle it however you like.

I am not proficient with PHP yet so I can only be general in the approach.

If you have the entire content of the text file in one variable you can parse through it looking at the line feeds to mark the next line and essentially do the same thing as above.


Stamp out, eliminate and abolish redundancy!
 
There may be a more graceful of doing it but off the top of my head I would read the file into and array with file(), then use array_search(), to get the index of 'COUNT_END'. Minus one from that and you can grab the data needed.

Example:
Code:
$file_lines = file('file01062005.txt');
$index = array_search('COUNT_END')-1;
$data = $file_lines[$index];
The data you want will be contained within $file_lines[$index].

Again this is off the top of my head and I have not tested the code.

Itshim
 
Dang I feel silly for not suggesting an array since that is how I do it in ASP and Javascript.
I guess I was just thinking in more basic terms because I could not be specific about PHP methods. :)


Stamp out, eliminate and abolish redundancy!
 
Hi Itshim...

Currently that codes doesn't return anything...so what I have tried so far is to test parts of it.

if I do

$file_lines = file('file01062005.txt');
$num_lines = count ($file_lines);


echo ("Total lines in file: " . $num_lines);

It prints out the number of lines so I know that the file is getting read properly.

So then I tried:

$file_lines = file('file01062005.txt');
$index = array_search('COUNT_END')-1;
$data = $file_lines[$index];

print($index);

I get -1 do I have to put that in '''s or somthing I have looked up php's function array_search but no mention of counting back one record or that syntax.

Then I tried:

$file_lines = file('file01062005.txt');
$index = array_search('COUNT_END')-1;
$data = $file_lines[$index];

print($data);

At that point I get a blank page with nothing on it.

Any suggestions I will continue look at the php site.

Thanks for your help so far.

 
My apologies... in the code I posted the line
Code:
$index = array_search('COUNT_END')-1;
I forgot to add the second parameter, which is the array the function needs to search, try...
Code:
$index = array_search('COUNT_END', $file_lines)-1;
 
If $file_lines is your array, how are you referencing that array to get the index number for COUNT_END?
I do not know the syntax for PHP but I think you need to say something more like:
$index = array_search('COUNT_END', $file_lines);
Otherwise it does not know what array it is searching so it cannot find a match. That explains why the previous attempt only returned -1 because the array_search returned null then did -1.


Stamp out, eliminate and abolish redundancy!
 
Dang, beat me to it.


Stamp out, eliminate and abolish redundancy!
 
Also, running the code I posted you should have received this error:

Warning: Wrong parameter count for array_search() in file.php on line xx

If you are testing on a development box, make sure you have error reporting set to E_ALL and php is set to display errors. It will make life easier than having to check the error log every time something doesn't work as expected.
 
I was just checking the log file to see after I made the changes above and this is what I have:

PHP Notice: Undefined offset: -1 in AW.php on line 5

So I took it out the -1 from the line and then ran the file and it returned the first line of the file so I am thinking that the search fuction is not working as advertised.

Becuase without the -1 shouldn't it return COUNT_END?



 
Write out the index number rather than the content in that array position and see what the number is.
Prior to PHP 4.2.0 it returns null instead of false.

Also do a print(count($file_lines)); (syntax?) to see how many lines are in the array.

The search is case sensitive so make certain the case is the same in the file as in the search.




Stamp out, eliminate and abolish redundancy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top