Parsing Line Breaks in PHP/JavaScript
Posted
by Matt G
on Stack Overflow
See other posts from Stack Overflow
or by Matt G
Published on 2010-03-31T03:05:56Z
Indexed on
2010/03/31
3:13 UTC
Read the original article
Hit count: 456
php
|JavaScript
I have a text area in my PHP application where users can enter notes in a project. Sometimes this is displayed on the page via PHP and sometimes it is displayed via javascript. The problem is, if the note is across multiple lines (i.e. the user presses enter while entering notes in the text area), it causes the JS to fail. It's fine when it's being done by the PHP.
The line of code in question is:
var editnotes='<textarea class="desc_text" style="width:20em;" id="notes_editor"><?php print $notes; ?></textarea>';
So, if the note is over multiple lines, the PHP builds the pager as:
var editnotes='<textarea class="desc_text" style="width:20em;" id="notes_editor">This is
a test note
over multiple lines
</textarea>';
And this obviously causes problems for the js. So my question is, what can I do to prevent this? As the code is being built by PHP before it even gets to the browser, I'm thinking that the best approach may be to parse it in the PHP so that the output is something more like this:
var editnotes='<textarea class="desc_text" style="width:20em;" id="notes_editor">This is<br/>a test note<br/>over multiple lines<br/></textarea>';
Will this work? How would I do it?
Thanks
© Stack Overflow or respective owner