Php remove BR-tag - php

I'm trying to remove the <br /> in output from database but this isn't really working out for me. My code starts with nl2br, which produces <br />. But I will allow my users to edit the texts and I would love to make sure that it line breaks are allowed in editing too.
But in my <textarea> it prints out the <br /> tag. All I want is a line break.
echo nl2br(preg_replace("/<br\W*?\/>/", "", $row["content"]));

Don't run the nl2br that is outputting brs. Also use \n in the replace value so you get new lines.
echo preg_replace("/<br\W*?\/>/", "\n", $row["content"]);
You also might want to use \h or \s instead of \W.

As I understand, there is two forms that you would like to use, when you want to display the text entered in a textarea, then you need the nl2br, which inserts br tags into the text. You may also want to save the contents to a database this way.
When you want to load the text back to a textarea, then depending on how you saved the text:
if it is saved with newlines: do nothing with the text just load it into the textarea
if it is saved with br tags: you need the preg_replace(...) part of your code with '\n' as the replacement (preg_replace("/<br\W*?\/>/", "\n", $row["content"]);).

Related

PHP output string and maintain spacing [duplicate]

Any ideas why formatted text from DB, when echo-ed out in php loses its formatting, i.e. no new lines? Thanks!
Use nl2br().
New lines are ignored by browser. That's why you see all text without line breaks. nl2br() converts new lines to <br /> tags that are displayed as new lines in browsers.
If you want to display your text in <textarea>, you don't need to convert all new lines to <br />. Anyway, if you do it... you will see "<br />"s as text in new lines places.
Because there are no html tags for formatting!
Try the nl2br function.
You could try add nl2br() function...
something like this: echo nl2br($your_text_variable);
It should work ;-)
The reason
This is the default behavior for all user agents. If you look at the page source, you'll see that your text has the same formatting like the one in the database (or textarea).
The reason of your confusion is probably that you once see the text in the <textarea> tag, which displays preformatted text, does not interpret the tags, and in the other case the text is interpreted (whitespace is not important in this case).
The browsers don't display new lines, unless specifically asked for - using <br> tag or any block level tags.
No tags == no new lines.
The fix
If you store preformatted text in the database,
you should wrap the output in the <pre> tag.
You may want to convert the formatting characters to the HTML tags you need using set of functions like nl2br, str_replace etc.
You may also correct your structure to store the HTML in the database instead of just plain text (however markup looks like a better solution).
See similar question:
How do I keep whitespace formatting using PHP/HTML?
The difference between the two images you show is that one has the text in a <textarea></textarea> and the other does not ... if you want 1:1: <textarea><?php echo $yourVariable;?></textarea>
It does output what you say to output. If the text is pre-formatted, put it inside the HTML <pre></pre> tag in your output script.
This should be helpful in answering.
How do I keep whitespace formatting using PHP/HTML?enter link description here
Set up a string preprocessing code for both input to database and output to display page

Want to get <br> tags from textarea, though no other HTML at all

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.

PHP - How to print '\n', '\r', '\r\n', and tabs in a text submitted by form?

All,
I am trying to build a completely UTF-8 web app which allows users to submit text content.
If I store a text copied on a web form (textarea) into a PHP string $str, and then print this variable, the text is obviously printed without the new lines, carriage returns, or tabs.
Is there a way for me to view those special characters when I print the text?
For example, if the user were to enter:
Two roads diverged in a yellow wood,
And sorry I could not travel both
I'd like to be able to print:
Two roads diverged in a yellow wood,\nAnd sorry I could not travel both
Either echo them into a textarea, or surround them with <pre> tags. Otherwise, massage the data with nl2br and a custom one for handling tabs:
function tab2nbsp($str)
{
return str_replace("\t", ' ', $str);
}
When you copy over text it should retain carriage returns and new lines in the source code and in any file/database you store the information in. If you're asking how to display it on an HTML page so that it renders with line breaks, try the php nl2br($string) function.
use PHP's nl2br to add <br />-tags to your newlines. http://php.net/manual/de/function.nl2br.php
you can also put the content in <pre>-tags or use the CSS-property white-space:pre http://www.css4you.de/Texteigenschaften/white-space.html
Use the <pre> tag to display a block of pre-formatted text (text with newlines, tabs, or multiple spaces). This tag preserves these elements.
The function nl2br() may also be of interest to you.
use this, when record entered in database this will convert br to nl so i we get back this to html so we have to convert it nl to br....
nl2br($str);

Why does PHP echo'd text lose its formatting?

Any ideas why formatted text from DB, when echo-ed out in php loses its formatting, i.e. no new lines? Thanks!
Use nl2br().
New lines are ignored by browser. That's why you see all text without line breaks. nl2br() converts new lines to <br /> tags that are displayed as new lines in browsers.
If you want to display your text in <textarea>, you don't need to convert all new lines to <br />. Anyway, if you do it... you will see "<br />"s as text in new lines places.
Because there are no html tags for formatting!
Try the nl2br function.
You could try add nl2br() function...
something like this: echo nl2br($your_text_variable);
It should work ;-)
The reason
This is the default behavior for all user agents. If you look at the page source, you'll see that your text has the same formatting like the one in the database (or textarea).
The reason of your confusion is probably that you once see the text in the <textarea> tag, which displays preformatted text, does not interpret the tags, and in the other case the text is interpreted (whitespace is not important in this case).
The browsers don't display new lines, unless specifically asked for - using <br> tag or any block level tags.
No tags == no new lines.
The fix
If you store preformatted text in the database,
you should wrap the output in the <pre> tag.
You may want to convert the formatting characters to the HTML tags you need using set of functions like nl2br, str_replace etc.
You may also correct your structure to store the HTML in the database instead of just plain text (however markup looks like a better solution).
See similar question:
How do I keep whitespace formatting using PHP/HTML?
The difference between the two images you show is that one has the text in a <textarea></textarea> and the other does not ... if you want 1:1: <textarea><?php echo $yourVariable;?></textarea>
It does output what you say to output. If the text is pre-formatted, put it inside the HTML <pre></pre> tag in your output script.
This should be helpful in answering.
How do I keep whitespace formatting using PHP/HTML?enter link description here
Set up a string preprocessing code for both input to database and output to display page

Escape all HTML except <br>

I am trying to display comments on a page and am having some trouble.
There are essentially two different types of comments I am trying to handle:
(1) The XSS type.. e.g. <script type="text/javascript">alert('hi')</script>. This is handled fairly easily by escaping it before it gets into the database and then running stripslashes and htmlentities on it.
(2) The comment with <br> breaks in it. When the data is stored into the database, I am running nl2br on it so the data looks like hi<br>hello<br><br>etc. However, when I display this comment, the <br>s do not turn into page breaks like I want them to.
Any idea what to do? I should note that turning off htmlentities fixes the second type, but the first type then is executed as pure html and displays an alert dialog.
Thanks,
Phil
If you want to remove unwanted tags you can try strip_tags. It supports allowable_tags so you can specify any tags that you don't want to be stripped. A sample from the manual:
// Allow <p> and <a>
// you can add <br> if you want it not stripped
echo strip_tags($text, '<p><a>');
So after you've converted all \n to be line breaks you dont have to worry about it being stripped. May not be what you want but hope it gives an idea.
One method: Replace <br> with a placeholder, like \n. Then do htmlentities to clean up html code. Finally, replace \n back with <br> to recover the line breaks.

Categories