I've a form where I create different texts. To use line breaks correctly I'm sending the input through nl2br. It works perfectly, using this code:
$formSubject = nl2br(htmlentities( $_POST["formSubject"], ENT_QUOTES, 'UTF-8'));
The problem I'm having is that I also have an Edit function and when I open a text to edit I get the line breaks but also <br/> in the text (which I don't want because the users of this will not be familiar with html) and if I save (even without changing anything) it will add another line break (which means double <br/><br/> if I open it to edit again. Is there any solution to this?
you can remove <br/> (str_replace()) or all html tags (strip_tags()) before edits
or
you can use WYSYWIG Editor (like tinymce or ckeditor) to edit text with html tags
Related
I'm using below line to remove text formatting from ckeditor result text area for some reasons.
html_entity_decode(strip_tags($result_string_from_ckeditor),ENT_QUOTES,'utf8');
It works great except than it stripping new lines and all separate lines will be join to each other.
I tried solution in this post and used below line, but no success
html_entity_decode(strip_tags(nl2br($result_string_from_ckeditor),'<br>'),ENT_QUOTES,'utf8');
Edit: for example
ckeditor source text:
<p>This is an example</p>
<p>of problem in strip_tags</p>
Source result after using command:
<p>This is an exampleof problem in strip_tags</p>
Many thanks.
Good morning,
Here's the problem:
I have some text being entered in via text editor (WYSIWYG/TinyMCE) and being displayed elsewhere as posting. The problem we have is that the text looses its formatting when being displayed as a posting. After digging through the code, I discovered that this was being done with a strip_tags() + echo preg_replace() combo. I'm still new to PHP, but I was able to figure out:
strip_tags() was taking out the formatting (b/c that's how it rolls)
I could add and to get the bold and italicized text to display
the underlined and strikethrough text are CSS styles and adding the code (as it is saved on the db table) to the strip_tags() list did NOT solve the problem
My question is: can I modify the existing code to solve this, or should I use something else (htmlentities() perhaps)?
EDIT: I tried htmlentities and it failed.
EDIT: I added just the tag and the problem is 50% solved. My text is underlined, but it shows lower than the non-underlined text that comes after it. Its as if the underlined text is being treated as subtext or something.
code snippet:
<div class="display_text_area">
<?php $text = strip_tags(str_ireplace("</p>", "</p><br/>",
$text_detail->description),
'<font><ul><li><br/><strong><em><span style="text-decoration: underline;">'); ?>
<?php echo preg_replace('/(<br[^>]*>\s*){2,}/', '<br/>', $text); ?>
</div>
I'm leaving the tag here to show that (a) I tried it, and (b) it didn't work. So (c) I know it needs to be removed or modified.
Many thanks in advance.
The point is that TinyMCE returns nominally valid rich HTML that doesn't need stripping or escaping before being used in an HTML page. However, you can't assume that the TinyMCE editor is running on the client, as a you might be exploited by someone who simply directly posts a response which contains an XSS attack.
IIRC, TinyMCE returns XHTML by default. You need to ensure that any returned HTML is correct using a library such as HTML Purifier.
I'm doing a web builder and I'm having a bit of trouble.
I want to have a textarea that you can enter text that you want to your TEXT element. This element I disabled the HTML when it´s later off previewed by simply putting a .innerText before posting preview. Though, I might just need the <br> tag.
I can use PHP or JS. Any ideas?
Use nl2br(htmlspecialchars($str)) when displaying the text. Note that the order of the function calls matters - first you escape all HTML in the string and then you convert linebreaks to HTML linebreaks.
When you are typing in a textarea, and the return key is pressed,
What actually goes on behind the scenes is this
Hi, \n There
which produces the following in the textarea.
Hi
There
Hence, what you would need to do is essentially change the \n (newline) to break tags.
http://php.net/manual/en/function.nl2br.php
Just use a plain textarea, afterword use a function like nl2br when you display it in your html page
nl2br(htmlspecialchars($string))
changes the line breaks to <br /> tags, so you can display them in html as seen in the textarea.
I have a situation where users can enter a question and this is written into a mysql table.
I then show this text in an admin panel for reivew and reply.
I can see the text in the admin panel has the same new lines (chrome developer) but it shows as one block of text (no new lines)...
I'm currently using to show this text... I've tried a but it still hows without line breaks.
How should I go about displaying this text with the new lines it contains? Is there a HTML tag to do this or do I need to use PHP and sub \n for br tag?
example below:
<span class="description">txthere
txthere
txthere
txthere
txthere.</span>
On display you can use CSS to style the HTML with the original line breaks, try white-space:
white-space: pre-line;
if it is textarea or console basically it puts a line break using /n which is not recognized by the browser as line break, you need to convert /n to <br/> to tell the browser that this is a line break, use this php's function
nl2br();
more information here: http://in.php.net/nl2br
this will convert all your /n tag to <br/> tag so that browser process it as line breaks
I have a problem with the following:
I want to make a page that gets a file (I upload it), reads it and outputs it in an html file.
I am uploading the file and saving the contents in a mysql DB just fine, but when I show it again, I don't have any <br />'s there (maybe because the file should have \t\n or something.
How can I make it show it like it was originally written. (In the DB I see it with the fine spacing).
You probably want nl2br(). It will transform all line breaks to <br>s
You can either wrap inside <pre></pre> tags to display it as it is, or better yet use nl2br() function to add html break lines <br /> before any newline/carriage return /r /r/n /n
Are you sure the problem isn't just in the HTML? Multiple whitespaces convert to one in web browsers. In modern browsers, you can use the CSS white-space property to prevent that.
body { white-space: pre; }
Alternatively, you could wrap that section of HTML in a <pre> element, or you could hardcode extra spaces into
at time u store file data in database encode data using htmlentities() and at time of displaying decode it using html_entity_decode()