Page 1 of 1

Cout up to line in a file

Posted: Fri Sep 12, 2003 4:18 pm
by Pineriver
I would like php to count the number of lines in a file, but only up to a string. like say I have this in a text file...

This is a line
This is another line
This is another line is the text file
And yet this is another line

I want php to be able to start from the top of the file and count down untill it reaches "This is another line is the text file" and output the number "3" since that is the line which has the string
Can anyone figure out how to do this?
Thanks in advance

Posted: Fri Sep 12, 2003 4:33 pm
by d-m
<?php

$inFILE = "/tmp/inputfile";

$source = fopen($inFILE,"r");
$i=0;
$buffer = fgets($source, 38 );
while ( ($buffer != "This is another line is the text file") || (!feof ($source)) ) {
$i++;
$buffer = fgets($source, 38 );
}
fclose ($source);
echo "The number of lines until This is another line is the text file is:" . $i;

?>

PS: 38 == number of chars that you have at the stop string.
$i == the number of lines of the file. until the string or the end of file.
$inFILE = is the file that you wanna open and get the content like "/tmp/file"

I Hope that could help U!

Posted: Fri Sep 12, 2003 4:49 pm
by Pineriver
Thanks for your help, but I get Parse error: parse error, unexpected T_BOOLEAN_OR AT Line 8

Posted: Fri Sep 12, 2003 4:59 pm
by d-m
it was: while ( $buffer != "This is another line is the text file") || (!feof ($source)) ) {

now its correct while ( ($buffer != "This is another line is the text file") || (!feof ($source)) ) {

Posted: Fri Sep 12, 2003 5:07 pm
by d-m
I did NOT put the program to run until now ... I was just passing the idea... but now I Runned IT and put it to Work

Here is the program running :)


<?php

$inFILE = "/tmp/inputfile";

$source = fopen($inFILE,"r")
or die ("Could not open the file");
$i=0;
$buffer = fgets($source, 38 );
while ( !feof ($source) ) {
$i++;
if ($buffer == "This is another line is the text file") {
break;
}
$buffer = fgets($source, 38 );
}
fclose ($source);
echo "The number of lines until This is another line is the text file is:" . $i;

?>

Posted: Fri Sep 12, 2003 5:58 pm
by Pineriver
Yes, it works, but when I try it on other files I get the total number of lines, and if there is numbers in the file the php stops responding.
Also I would like to know how to have the script stop at a line that has these characters [" . , ] Thanks alot for your help

Posted: Fri Sep 12, 2003 11:17 pm
by scorphus
You could try this:

test.php:

Code: Select all

<pre>
<?php
$str = 'This is another line is the text file';
$lines = file('data.txt');
$n = 0;
foreach ($lines as $key => $value) {
	$n++;
	if (strstr($str, chop($value))) // we'll wanna stop case $str is part of the iteration $value
		break;
}
if (current($lines)) { // current() will return false if the last foreach reaches the end of $lines
	echo 'Found an occurrence of "' . $str . '" in line ' . $n . ":\n";
	echo '$lines[' . $n . '] = ' . chop($lines[$n]) . "\n";
	echo "Continue showing the rest of file:\n";
	for ($i = $n + 1; $i < count($lines); $i++)
		echo '$lines[' . $i . '] = ' . chop($lines[$i]) . "\n";
}
else
	echo 'No occurrence of "' . $str . '" was found in the file, sorry.' . "\n";
?>
</pre>
data.txt:

Code: Select all

This is a line
This is another line
This is another line is the text file
And yet this is another line
Again a new line
And another line
A simple Line
Output:

Code: Select all

Found an occurrence of "This is another line is the text file" in line 2:
$lines&#1111;2] = This is another line is the text file
Continue showing the rest of file:
$lines&#1111;3] = And yet this is another line
$lines&#1111;4] = Again a new line
$lines&#1111;5] = And another line
$lines&#1111;6] = A simple Line
Script lines that are not self explained are commented. I hope it helps!

Regards,
Scorphus.