textfile in string

41 views Asked by At

all i want is to display the contains from pageset.txt line by line.

ok i have this code:

$lines = file('pageset.txt');
foreach($lines as $line) {
// do something with $line.
$hh = $line . "<br/>";

echo $hh;

now i want to display it line by line as you see there is
so it means each contains should display line by line and so it must however it doesnt display line by line and the bad thing is it only display one contains from pageset.txt. hope someone here could help, thank you in advance.

2

There are 2 answers

0
Niet the Dark Absol On BEST ANSWER

You keep overwriting $hh before echoing out the last line.

Try this instead:

echo nl2br(htmlspecialchars(file_get_contents("pageset.txt")));
  • file_get_contents to read in the whole file in one go (unsuitable for extremely large files, but still better than file in that regard).
  • htmlspecialchars to escape any HTML in the file so that, in particular, < is shown correctly. Remove if you actually want to include the file's HTML.
  • nl2br is a haxy method of adding <br /> to the end of each line. Essentially it's a shorthand for str_replace(PHP_EOL,"<br />".PHP_EOL,$input).
0
John Conde On

You forgot to add the concatenation operator to your variable assignment:

$hh = '';
$lines = file('pageset.txt');
foreach($lines as $line) {
    // do something with $line.
    $hh .= $line . "<br/>";
}
echo $hh;