tags:

views:

90

answers:

2

EDIT: Ok, thanks for your help guys. Using zerkms' suggestion, I have managed to get <br> tags inserted instead of carriage returns, so it is displayed properly on the page and doesn't break the javascript. The problem is now that when the user clicks on this text, it becomes a textarea and is therefore inline editable. However, the text string now has the tags displayed in it, rather than just having the editable text over multiple lines.

Any suggestions?

ORIGINAL QUESTION: 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

A: 

as of nl2br() didn't help second proposal is append \ to all line breaks. in some kind of manner:

preg_replace('!([\r\n]+)!', '\\\$1', $text);

to be clear, the sample:

var baz = 'foo
bar';

now become:

var baz = 'foo\
bar';
zerkms
Thanks, I'm not sure if that will work, as the text is coming from a MySQL text field, so I'm not sure the line breaks are stored in the string as "\n"
Matt G
Ok so I tried using this, and it adds <br> to the string but does not remove the line break, so I end up with:a test note<br>over multiple lines<br>
Matt G
oops, it's really not removed line breaks, sorry then :-(
zerkms
CLARIFICATION: This doesn't seem to allow me to put comments over multiple lines, but the string starts in a new line after each <br>, as well as including the tag.
Matt G
@Matt: i updated my answer, now it would help
zerkms
Thanks zerk, that didn't work for me either. I'd need the output to be more along the lines of var baz='foo<br>bar';. I've managed to get it to that stage now using ereg_replace, but the problem is that the html now doesn't render the line breaks in the textarea where the <br> tags are!
Matt G
i'm confused %) if the <br> instead of line breaks is enough then just preg_replace [\r\n]+ with <br>.
zerkms
Thanks for your help, I've done that (using your code) and it's now replacing the line breaks with <br>, so my original question is now resolved. It's not rendering the line breaks in the textarea though, just putting a space, but I think that now makes my problem an HTML question.
Matt G
A: 

Use json_encode() to output a value for javascript.

var editnotes = <?php echo json_encode($str); ?>;
chris
I've tried this, it just seems to put quotes around the text.
Matt G
Are you suggesting that it doesn't produce a valid javascript string?
chris