HTML displaying articles - php

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.

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>");

Proposed Answer:
Use <span></span>
Tags like <p></p><div></div> take up as much space as they can, while <span></span> takes up as little as it can to hold whatever is inside it, so it might be more suitable for you.
Let me know if that worked for you.

In PHP you can use function nl2br that changes all newline characters to BR HTML tag. http://php.net/nl2br

Related

textarea that contains pre code security

What is the most secure way to save data from a textarea that contains a <pre><code> text in it? , using strip_tags will remove all the tags from the text..
is it save to use this:
strip_tags($input, '<pre><code><other accepted tags except script,php,...');
or should I do other things too?
What is the most secure way to save data from a textarea that contains a <pre><code> text in it?
Save it as it is.
When you take that data back out of the database and put it into a web page, call htmlspecialchars on it first to escape it so that it looks like normal text on the page.
If you want the user to be able to input actual markup, but you only want to allow certain tags, then you've got a different problem and you want something like htmlpurifier.
Either way, the input or database layer is not the right place to be worrying about output formatting concerns.
If you are saving the contents of the text area to mysql database you should use mysqli_escape_string. before saving the data.
Also you can remove javascript tags from the posted data using regular expression. e.g preg_replace

Form add new line on enter, set up for sql

There are a lot of little pieces of information on this subject, and maybe I'm just not looking in the right spot. But I'd like to put it all together in one place. Start to finish.
I'm trying to make a form pretty much like the one I'm using right now on stack overflow. There is a textarea, when someone hits enter, it does not submit the form, but adds a new line. But it doesn't display \n or <br \> in the actual text box.
Then the text with all structural integrity needs to be entered into a database and retrieved to an xhtml page.
I've hear of using javascript. I've heard of the nl2br function. They all seem to be pieces to the puzzle, but I can't find the big picture.
Does anyone have a sample script of start to finish how they would accomplish this?
Thank you very much for your help! I hope others can use this too who are having difficulty.
You need to create a regular <textarea>, and save its raw text to the database. (making sure to use parameters to prevent SQL injection)
Then, when you want to display the text, use <pre> or nl2br() to ensure that the newlines are displayed by the browser.

Text area to modify HTML - rendering line breaks

I'm loading a portion of an HTML page into a text area so that I can make small changes. All the HTML tags are shown along with the text, which is what I need to happen; I don't want a WYSIWYG editor or anything fancy.
The one thing I want is for line breaks to be shown in the text area in addition to the <p></p> and <h1></h1> tags otherwise it's a giant wall of text and it's really hard to proof read. I don't want the line breaks to be doubled after I save the modifications though as the next step will be to convert everything in the text area to a PDF file.
ETA: nl2br() doesn't work because there are no line breaks to begin with. The content is assembled from paragraphs in a MySQL database using a loop. The tags are inserted during the loop too.
What's the best way to do this? I'm using PHP.
Oh, PS - I'm aware of the security concerns of not stripping the tags. This page is for the admin (me) only and will be password protected.
Maybe you can filter those first, before showing to textarea? Something like this, maybe (add newline after the closing tag):
$rawhtml = str_replace(array("</p>", "</h1>"), "\n", $rawhtml);
Before sending data to your textarea put your variable in nl2br() func then send it to your textarea.

PHP MySQL custom blog, formatting post

I'm creating my own blog in PHP and want to know your opinions on how I should format my post content.
Currently I store the post content as just plain text, call it when necessary, then wrap each line with P tags. I did this in case I wanted to change the way I formatted my text in the future and it would save me the dilema of having to remove all P tags from the posts in the DB.
Now the problem I have this this method is that if I want to add extra formatting in, e.g. lists etc those would also be wrapped with P tags which is not correct.
How would you do this, would you store text as plain text in the DB, or would you add the HTML formatting and store that in the DB to?
I'd prefer not to store unnessary HTML in the DB, but not sure of a way around it?
I think the best way would be to keep the html in the db. You would have too much to work with parsing the text if you don't use html.
See how it's done in other blog tools. I know that Joomla, for example, keeps all html in the db. I know Joomla isn't blog tool :) but still...
Wordpress stores html in the db. You say you are concerned about storing 'unnecessary' html in the db. What makes it unnecessary? I think it is the opposite. You may have headings or bold or italic text in your post. If storing as plain text, how do you save this formatting? How are you saving the lists you mentioned?
I see it as a better practice to store raw user input in the database, and format it on output, caching the result if it is needed. That way you can change the way you are parsing things easily without having to regex-replace anything inside the database. You can also store the raw input in one column, and the formatted HTML in another one.
I assume that you are formatting your raw text with the Markdown or the Textile syntax?
If you store HTML in your DB, you will be just a few clicks away from your current situation:
you can use strip_tags() to remove HTML formating and in case of bigger changes, you can run HTML Tidy on your code to remap tags and classes.

PHP:HTML markup breking when taking first 200 chars

in my PHP web application,I have a dataentry form where users will enter data using a rich text editor (FCKEditor i m using) and will be saving the Markup from the editor to the DB table.In another page i have to display the first 200 chars of the content (with View more link to view the entire thing). So when i m taking first 200 chars,the HTM Lmarkup is breaking because i may miss the closing tags of some of the html tags started already.How can i get rid of this ? I know i can use strip_tags to remove all HTML markup.But i wanna keep that as it is.Is there anything which i can do to solve this ?
Run it through HTMLTidy as that might help. For example, when you have link tag (a) opened but not closed, that might help to get rid of the link "bleed" to next element. You will still have issues if your script cuts the string from the middle of the tag, a la "<di". It's not fool-proof solution and i wouldn't rely on it.
The best practice imgo is to treat the "short" version of the text separately, just let the user enter it into separate text editor.

Categories