2

I have a text file with lots of text, and I want to display a part of it on the screen with PHP.

At a certain point there's string like ITEM DESCRIPTION:

What I want to get all content from this string (just after it) to the end of the file.

This is my code so far:

$file = "file.txt";
$f = fopen($file, "r");
while ($line = fgets($f, 1000))
  echo $line;

:)

3 Answers 3

6

How about you use strstr() and file_get_contents() ?

$contents = strstr(file_get_contents('file.txt'), 'ITEM DESCRIPTION:');
# or if you don't want that string itself included:
$s = "ITEM DESCRIPTION:"; # think of newlines as well "\n", "\r\n", .. or just use trim()
$contents = substr(strstr(file_get_contents('file.txt'), $s), strlen($s));
Sign up to request clarification or add additional context in comments.

Comments

4
$file = "file.txt";
$f = fopen($file, 'rb');
$found = false;
while ($line = fgets($f, 1000)) {
    if ($found) {
       echo $line;
       continue;
    }
    if (strpos($line, "ITEM DESCRIPTION:") !== FALSE) {
      $found = true;
    }
}

1 Comment

Woops. sorry. the strpos arguments are reversed. I'll edit the answer.
1

What about

$file = "file.txt";
$f = fopen($file, "r");
$start = false;
while ($line = fgets($f, 1000)) {
  if ($start) echo $line;
  if ($line == 'ITEM DESCRIPTION') $start = true;
}

?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.