Search code examples
phphtmlmysqlweb-applicationspre

HTML displaying articles


Hey guys Im building a web-app where users can login and post/read articles and comment and things.

Im giving them a form to post an article where they provide its title, description and text.

leaving the validations and sql injections aside (already done that), I need help with displaying the article stored in MySQL database as TEXT.

Im taking the article text from a textarea, and displaying it in a p tag but then obviously it skips the new line characters entered by the users, but the pre tag makes it ugly by giving a wide scrollable display.I want to know which tag is appropriate to be used for this purpose? or is even taking an article through textarea correct?

Im a learner and am building such a webapp with articles and comments sections for the first time, so any suggestions are most welcome. Thank you in advanced.


Solution

  • My recommendation would be of two choices:

    1. Use Plain Text: If you want that user can not put any HTML in the contents, show a simple HTML Textarea input to user, then when the user enters a new line (Enter key) it would be \n in your database. When you want to print the article just use nl2br($article_contents); and it will convert the new lines (\n) into HTML line breaks.

    2. Rich Text: If you want users to put HTML contents in article then it would be easy if you use any Text Editors like TinyMCE. TinyMCE will make it easy for your users to do simple HTML Formatting like headings, bold, italic, paragraph alignments, color, add images. Then in the PHP side use strip_tags function to allow only the certain tags so the user could not insert any malicious code like XSS injections into HTML contents. For example:

    strip_tags($article_contents, "<u><b><i><font><span><p>");