开发者

read a text file with PHP and display all text after a certain string

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 ($lin开发者_运维问答e = fgets($f, 1000))
  echo $line;

:)


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));


$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;
    }
}


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;
}

?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜