0

I have a TXT file with 8000 lines. I need to find the first 20 lines that contain the "keyword". But I need my script to start searching the file from line 100. Not from the beginning. I tried this, but it doesn't work.

<?php
$i=100;
$search = 'keyword';
$lines = file('mytextfile.txt');

foreach($lines as $line)

{
if($i>=120) break;
  if(strpos($line, $search) !== false)
 
    echo $line;
        $i++;    
}
?>

The script works but searches the file from the first line.

I also tried different script:

<?php
$ln = 0;
$start = 99;
$end = 119;

$fd = fopen('mytextfile.txt', 'r');

while(true) {
    $line = fgets($fd);
    if(!$line ||  $ln === $end + 1) {
        break;
    }
    if($ln >= $start && $ln <= $end) {
        echo $line;
    }
    $ln++;
}

fclose($fd);
?>

This script reads the file from line 100 to 120, but I don't know how to add a function to it to find only lines that contain a keyword. Can you help me?

I figured it out:

<?php
$keyword = 'keyword';
$lines = file('mytextfile.txt');
$lines = array_slice($lines, 100);
$i = 0;
foreach($lines as $line) {
    if(strpos($line, $keyword) !== false){
        echo $line;
        $i++;
        if ($i >= 20) {
            break;
        }
    }
}
?>

Thank you very much miken32 for you help. I couldn't do it without you.

  • 1
    So, once you have your file line in a variable, all that's left is to search if contains your desired substring. That topic has been well covered both on and off the site. Have you run into a specific issue with this? – El_Vanja May 26 '21 at 15:10
  • Your attempts don't really make sense; for your first one, you set a counter to 100 but it has no effect on what lines are checked. You're also incrementing that counter with every line, instead of just on the matches. What can be helpful when beginning is writing out the program in plain language first, and then writing the code to fit those rules. E.g. "I want to put the lines into an array. I'm going to remove the first 100 lines, and then loop through the rest. On each line I'm going to check for a keyword, echo and increment a counter if I find it. If the counter hits 20 I will stop." – miken32 May 26 '21 at 15:20
  • Also there are more elegant ways to do this with e.g. `array_search` but this looks like homework so you should stick with what you've learned so far. – miken32 May 26 '21 at 15:23
  • I've been looking for a solution for three days and I'm still failing. I'm not a coder. I am a webmaster and need such a script for my site. I really don't know how to do it. – René Kubiczek May 26 '21 at 15:24
  • https://3v4l.org/3ONSS – miken32 May 26 '21 at 16:27
  • Thank you miken32. I tried it, but it doesn't work. I double checked the path to my txt file. I tried the relative path as well as the absolute path. I added $keyword = 'keyword' to the code, but it still doesn't work. I don't see any error. Just white screen. I have no idea what's wrong. – René Kubiczek May 26 '21 at 17:09
  • White screen could just be because error reporting is switched off in PHP. maybe check the PHP error log file, and/or configure error reporting and/or logging to report all errors and warnings – ADyson May 26 '21 at 17:58
  • I figured it out. I hope this helps other people Thank you very much for you help. – René Kubiczek May 26 '21 at 18:04

0 Answers0