I am creating a news publishing website and the only thing I can't do in the site is enter and show new-line.
There is a textarea in the site to write articles, if I press ENTER while I am typing, it will take me into a new line, but after having submitted all, and make query to mysql to pull and echo what I had typed in, the new line does not show up. All texts are next to each other. So, I was wondering if i could input and output newlines in textarea with the help of PHP?
When you submit a form, you are submitting text.
When you put that text into an HTML document, it is HTML.
In HTML a new line is just a piece of white space with the same significance as a space or a tab.
If you want new lines to show up, then you need to with wrap them with markup that causes them to be rendered (such as a <pre> element or an element with the white-space property set to cause them to render) or you need to replace them with suitable markup.
A very primitive replacement would be PHP's nl2br function. A more advanced option would generate <p> elements and treat different numbers of sequential new lines differently (e.g. a Markdown parser such as the one used by Stackoverflow).
Related
I am displaying user submitted HTML as part of a page (the user's resume). I noticed that if the user's HTML get's cut off in the DB, due to character limit, when the HTML is displayed, any tags that didn't get closed, spills over to the rest of the page.
Am I able to contain the HTML within a div? Another solution? I'm using PHP, by the way.
Thanks.
If you have one ore more unclosed tags in you HTML, your DOM becomes invalid and the consequences are unpredictable. Make sure that the submitted HTML doesn't get cut by using a TEXT column in your DB table.
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.
I've installed CKeditor on a client's site that allows them to enter some text using a WYSIYG editor. It is locked down for the most part, only allowing bold, italic, unordered lists, etc.
I also run the user submitted HTML through HTML purifier to make sure they don't get smart and start trying to add tables, for example. It is also a good idea to limit what they can enter for XSS concerns, for example.
I just looked at some of the output produced by the CKeditor/HTML Purifier combo, and seen this atrocity...
<p>
<span style="font-size:11px;"><br /></span></p>
Is there anyway I can configure HTML Purifier or use something else to remove elements with no text node? It would obviously need to start at the latest descendant, and then run up the DOM tree, removing the most parent that has no elements with a text node.
Is there any edge cases to this approach, assuming there are never any presentational only elements in that markup? There are no self closing elements that will be present that I can think of (e.g. images, input elements, etc).
You can use PHP's strip_tags() function:
http://php.net/manual/en/function.strip-tags.php
This will delete all html-tags, except the ones you enter as the second parameter. In your case to allow bold, italic and unordered lis only this would be:
$text = strip_tags($text, '<b><i><ul>');
Sure there still can be empty ones after this, but you'll surely could get rid of the span and p tags with this.
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.
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