How to add line-breaks when echoing from a txt file using php?

So I have been starting on some php recently and made a page where people can type a text and then when they click save everyone on that page sees that text displayed under somewhere, after the text gets writen to the txt file by a php code I use another php code in my home page code that gets the text and ‘echoes’ it onto my page.

This code looks like that:

<?php

$bestand = fopen("tekst.txt", "r");
$tekst = fread($bestand, filesize("tekst.txt"));
fclose($bestand);
echo "<p style='
      margin-top: 5%;
      color: #04AA6D;
      font-weight: bold;
      text-align: center;
' id='tekst'>" . $tekst . "</p>";

?>

The tekst file looks like this:
Test
Test
Test
Test

But when it echoes it onto my page it all gets put into one string. like this:
Test Test Test Test

what is the easiest way to force php to add line breaks where they need to be.
Any help would be really nice as im just starting out on php and I really cant find a answer.

>Solution :

Following my previous comment and @Álvaro González comment, you can maybe try this:

$tekst = file_get_contents("tekst.txt");
$tekst = htmlspecialchars($tekst);
$tekst = nl2br($tekst);
echo "<p style='
      margin-top: 5%;
      color: #04AA6D;
      font-weight: bold;
      text-align: center;
' id='tekst'>" . $tekst . "</p>";

Leave a Reply