I am inserting this text What is <br/> PHP? into the database
Now I want to show this text as a line break. Like below:
What is PHP?
I am using PHP nl2br() function but it's not working. I am getting the value like this:
What is <br/> PHP?
How can I solve it?
Thank You.
A question comes up here... where are you inserting this text (string) ?
If you are injecting it as HTML you'll get the desired result in the rendered page.
I assume this is not the case: you want HTML line breaks turned into newlines.
So...
nl2br()converts newlines into <br />: that's the opposite you want to do
http://php.net/manual/en/function.nl2br.php
Just use str_replace:
$out = str_replace( "<br/>", "\n", $in );
Where $in is the input string and $out is the desired output
http://php.net/manual/en/function.str-replace.php
Just a couple of things to note:
1) the above code will work with HTML line breaks <br/>, not if you have <br>, or <br />
If this is an issue you may pass the function array of strings and array of their replacements. This is well documented in the link above.
2) If you use the code snipped I wrote above you'll end having two spaces in the resulting string:
What is (with trailing space)
PHP (with leading space)
Just use css instead of server-side transformations:
p {
white-space: pre-line;
}
<p>I am inserting this text What is
PHP? into the database
</p>
<p>Now I want to show this text as a line break. Like below:</p>
<p>What is
PHP?</p>
Related
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"]);).
I'm trying to make a list of email addresses (each one on a new line). I pull the array from my database, explode it along the comma delimiter, and run it through a foreach loop.
$emailsList = "";
foreach($emails as $email)
{
$emailsList = $emailsList . "\n" . $email;
}
echo "Additional Report Emails<textarea name='showReportsEmails'>".$emailsList."</textarea>";
When I look in the textarea itself, it will literally show the line break tag in the textarea. What can I do to get rid of this and have the text behave as I want?
<br />
email1#gmail.com<br />
email2#gmail.com
Thanks!
It looks like when the result is printed in the textarea, the PHP new lines (\n) are converted to <br/>s. So, maybe there's a nl2br() function being used to output the result into the textarea. It should be fixed by removing that. Otherwise, you can use the code below when setting the textarea's content:
<textarea><?php echo implode("\n", array_map('trim', explode("\n", strip_tags($emailsList)))); ?></textarea>
The code above first removes all html tags from the $emailsList variable, and then also removes white spaces before and after lines, so you would be good to go with this.
You should be using Carriage returns instead of HTML code in the textarea.
\r
The name suggest the the element can only hold text. It doesn't parse as HTML rather it does so as text.
I tested this code:
<?php
$value = "Hello,\nname\nis\nScript47.";
echo "<textarea>$value</textarea>";
?>
Output
Edit 1:
You could also use \r to see if it helps.
I know when saving a textarea you can use the nl2br() or str_replace to change the /n to br tags etc. However what im not sure about how to insert line breaks into a textarea. I cant seem to find much about putting the data back into a textarea with those line breaks.
For example I have a form where users can update fields. So the user may enter:
foo
bar
baz
When that is saved to the database it would be saved as:
foo<br />bar<br />baz<br />
Now when that user goes back to that form after a page refresh all the fields are automatically populated with their previous data by taking the data from the database.
However the textarea shows the br tags as text instead of adding in the line breaks. i also tried changing the br tags to /n hoping the textarea would interpret these as line breaks but no joy. As well as this I also tried escaping etc.
So my questions are can this be done? Or more importantly can it be done using HTML/PHP (im using smarty). If that isnt possible can it be done using javascript?
Examples would be appreciated.
thanks for reading
Don't do nl2br when you save it to the database. Do nl2br when you're displaying the text in HTML. I can strongly recommend to not store any HTML formatting in the database (unless you're using a rich HTML editor as well, in which case it would be silly not to).
A newline \n will just become a newline in the textarea.
You could use str_replace to replace the <br /> tags into end of line characters.
str_replace('<br />', PHP_EOL, $textarea);
Alternatively, you could save the data in the database without calling nl2br first. That way the line breaks would remain. When you display as HTML, call nl2br. An additional benefit of this approach is that it would require less storage space in your database as a line break is 1 character as opposed to "<br />" which is 6.
Ahh, it is really simple
just add
white-space:pre-wrap;
to your displaying element css
I mean if you are showing result using <p> then your css should be
p{
white-space:pre-wrap;
}
You can use following code:
$course_description = nl2br($_POST["course_description"]);
$course_description = trim($course_description);
Some wrong answers are posted here.
instead of replacing \n to <br />, they are replacing <br /> to \n
So here is a good answer to store <br /> in your mysql when you entered in textarea:
str_replace("\n", '<br />', $textarea);
My recommendation is to save the data to database with Line breaks instead parsing it with nl2br. You should use nl2br in output not input.
For your question, you can use php or javascript:
PHP:
str_replace('<br />', "\n", $textarea);
jQuery:
$('#myTextArea').val($('#myTextArea').val().replace(#<br />#, "\N"));
PHP Side: from Textarea string to PHP string
$newList = ereg_replace( "\n",'|', $_POST['theTextareaContents']);
PHP Side: PHP string back to TextArea string:
$list = str_replace('|', '
', $r['db_field_name']);
This works on me.
str_replace(array("\r", "\n"), '
', $textareavalue);
The simple way:
Use this to insert into mysql:
$msg = $_GET['msgtextarea']; //or POST and my msg field format: text
$msg = htmlspecialchars($msg, ENT_QUOTES);
And use this for output:
echo nl2br($br['msg']);
What I found works in the form is
str_replace('<br>', PHP_EOL, $textarea);
From PHP using single quotes for the line break worked for me to support the line breaks when I pass that var to an HTML text area value attribute
PHP
foreach ($videoUrls as $key => $value) {
$textAreaValue .= $value->video_url . '\n';
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HTML/JS
$( document ).ready(function() {
var text = "<?= htmlspecialchars($textAreaValue); ?>";
document.getElementById("video_urls_textarea").value = text;
});
I'm not sure this is possible but you should try <pre><textarea> ... </textarea></pre>
<?php
$smarty = new Smarty;
$smarty->assign('test', "This is a \n Test");
$smarty->display('index.tpl');
?>
In index.tpl
{$test|nl2br}
In HTML
This is a<br />
test
Usually, I use nl2br() and it does come out just like it's entered in the textarea, but this causes a problem when using bbcode or posting code in <code> or <pre> tags, since it adds extra line breaks.
For example this code
[sub-title]test[/sub-title]
some text here.
I'd like it to look like that when displayed in the browser, but because [sub-title] becomes <div class="sub-title"> the <br /> adds an extra line break, so it will look like this (with 2 line breaks in between)
**test**
some text here.
I haven't fully looked into it yet, but could the PHP bbcode parser help, or is the only/best solution to use regex?
You can use nl2br()
Example
$message = nl2br(preg_replace('#(\\]{1})(\\s?)\\n#Usi', ']', stripslashes($message)));
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.