简体   繁体   中英

how do I read a line break as \n from a .txt file using php

here is what I have:

<?php echo file_get_contents("thingy.txt") ?>

I am using file_get_contents() to get data from a file and put it on my website. this file contains line breaks though that are translated into simple spaces when I display it on my website. the file itself is a .txt, but I use html elements so that when it is read, the content is styled on my website.

I write to this thingy.txt file using a element in a form and submitting it, then using a seperate php file I write that input data to the .txt file. any suggestions how I can create the line breaks on my index file using my setup or do I have to change it?

\\n isn't going to be interpreted as a line break by a browser. All whitespace in HTML is condensed into a single space character. Use nl2br()

Take a look at this answer:
https://stackoverflow.com/a/9897022/1173391

this should be all you need

$file = new SPLFileObject('/path/to/file.txt');
foreach($file as $line) {
    echo $line;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM