How do I echo line breaks from a MySQL database?

前端 未结 2 1082
失恋的感觉
失恋的感觉 2021-01-24 05:55

I have created a simple wall post feature, like on FaceBook.

A user writes a post, the post is submitted to the database and then echoed back onto the website; that all

相关标签:
2条回答
  • 2021-01-24 06:16

    The answer(s) suggesting nl2br are great, I would go with that approach.

    In your original question I think there is some confusion -- users don't type \n every time they want a new line. The slash is for escaping (special character, not a normal n).

    On Windows machines, the Enter key is equivalent to \r\n where \r is carriage return and \n is new line. On Linux or similar, the Enter key is \n. \r's ascii value is 13 and \n's is 10.

    So an EOL (End of Line) will either be \r\n or \n. Php has the nl2br function whose purpose is to replace all \r\n or \n with <br>, the html tag for a line break.

    0 讨论(0)
  • 2021-01-24 06:19

    PHP function nl2br converts newlines to "<br>" breaks.
    So, $wallpost=nl2br($wallpost); should accomplish the task.

    0 讨论(0)
提交回复
热议问题