1

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 Answers 2

2

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).
Sign up to request clarification or add additional context in comments.

Comments

1

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;

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.