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
Cout up to line in a file
Moderator: General Moderators
- d-m
- Forum Commoner
- Posts: 31
- Joined: Fri Aug 29, 2003 4:24 pm
- Location: Rio de Janeiro - RJ - Brasil
<?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!
$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!
Last edited by d-m on Fri Sep 12, 2003 4:56 pm, edited 1 time in total.
- d-m
- Forum Commoner
- Posts: 31
- Joined: Fri Aug 29, 2003 4:24 pm
- Location: Rio de Janeiro - RJ - Brasil
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;
?>
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;
?>
- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
You could try this:
test.php:
data.txt:
Output:
Script lines that are not self explained are commented. I hope it helps!
Regards,
Scorphus.
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>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 LineCode: Select all
Found an occurrence of "This is another line is the text file" in line 2:
$linesї2] = This is another line is the text file
Continue showing the rest of file:
$linesї3] = And yet this is another line
$linesї4] = Again a new line
$linesї5] = And another line
$linesї6] = A simple LineRegards,
Scorphus.