Reply to Re: Reading text with newline from db via php and placing in client javascript variable

Your name:

Reply:


Posted by Randell D. on 07/27/07 08:11

On 26 Jul, 22:41, Rik <luiheidsgoe...@hotmail.com> wrote:
> On Thu, 26 Jul 2007 21:20:56 +0200, Randy Webb <HikksNotAtH...@aol.com>
> wrote:
>
>
>
> > Rik said the following on 7/26/2007 1:19 PM:
> >> On Thu, 26 Jul 2007 17:49:41 +0200, Randell D.
> >> <fiprojects....@gmail.com> wrote:
>
> >>> Folks,
>
> >>> I am cross-posting on purpose, even though my problem appears to be
> >>> javascript related there might well be a function unknownst to me that
> >>> will help resolve the problem before sending to the client...
>
> >>> Basically I have a textarea box that is saved from client via php into
> >>> my mysql database.
>
> >>> When I later try to restore the data I can see it in the HTML source
> >>> from my browser (I'm using latest version of Firefox) but an error in
> >>> the javascript console tells me the variable is not terminated... This
> >>> is because there is a newline character in my text.
>
> >>> I've tried addslashes and htmlentities in php but its not worked for
> >>> me - can someone advise me of what the solution/workround is?
> >> $string = str_replace(array("\n","\r"),array('\n','\r'), $string);
>
> > Shouldn't that be:
>
> > $string = str_replace(array("\n","\r"),array('\\n','\\r'), $string);
>
> > As simply replacing "\n" with '\n' doesn't escape the \ in the newline
> > sequence which is what is throwing JS as PHP is reading it as a newline
> > and breaking the line up before being sent to the browser.
>
> Nope, not in PHP: "\n" is a newline character, '\n' is just a literal
> slash+n. Double or single quotes make a difference there.
> --
> Rik Wasmus


Thanks - I never knew that the single or double quotes could make such
a serious difference... and your suggestion worked...

>> $string = str_replace(array("\n","\r"),array('\n','\r'), $string);

For anyone reading this post in the future they should take great care
to ensure they are using the correct source string - my data returns
from my DB in a multi-dimensional array and I was referencing it
incorrectly which resulted in no output. I had originally assumed the
above solution did not work but then I printed my source data before
and after the result of str_replace and discovered they were both
blank... simple mistake but possible even for the most talented of the
talented :)

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация