I was working on a piece of code and wanted to print out a php variable with innerHTML. The code was running fine untill there was a linebreak in the text that I was outputting via innerHTML.
As the next step I tried to use nl2br to replace line breaks with < br / >. The nl2br will replace the line breaks for < br / > but the < br / >s are placed on new lines like this:
$authorscomments=nl2br($row[authorscomments]);
"this is a comment< br / >
< br / >
comment continued"
This way I still have the line breaks and my jquery breaks. Any suggestions how to pre-treat my text comming from the database so that I can output it via innerHTML?
{document.getElementById("nexttable").innerHTML = "<br>AUTHORS COMMENTS <?php echo $authorscomments; ?>";}
Ok, so I figured it out. To pre-treat the text with php before it hits innerHTML this is what I did:
$authorscomments = str_replace(array("\r", "\n"), "<br />", $authorscomments);
This removes all the line breaks and replaces them with < br / > and innerHTML runs just fine at the same time it maintains the formating of the text from the database.
Thats very easy in PHP:
$authorscomments=nl2br($authorscomment);
That replaces all linebreaks with <br />
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.