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 textare
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;
});
<?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
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);
What I found works in the form is str_replace('<br>', PHP_EOL, $textarea);
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']);
You can use following code:
$course_description = nl2br($_POST["course_description"]);
$course_description = trim($course_description);