Jump to content


Photo

newline breaks


  • Please log in to reply
2 replies to this topic

#1 macdumbpling

macdumbpling
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 31 March 2006 - 05:21 PM

I currently have a working event calendar that has a javascript popup div when you mouseover the title of an event. A javascript function call includes php variables:
ShowInfo('$event[location]<br />$event[date]<br />$event[time] [$event[duration] $event[durLabel]]<br />$event[descrip]')
....you might notice that this script in itself would not work, it's just an example of passing my php variables into a javascript function). The problem with this is $event['descrip'] is a text box field from a form and can accept line breaks (in windows, the characters \r\n are line breaks, if you're unfamiliar). The problem lies when the html/javascript is sent to the browser and there is a line break in the $event['descrip'] variable. The code will show up like this:
1 | ShowInfo('1st Floor Conference Room<br />2006-07-09<br />9:30 am [30 min]<br />Topics include:
2 | Inventory, Lunch schedules, etc.')
The javascript function is broken by the \r\n line break, causing an error on that event popup.

If anybody can point me in the appropriate direction to correct this quark, I'd be most appreciative! I've currently tried utf8_encode and utf8_decode (because I wasn't sure which it would be) and replacing the '\r\n' using str_replace() but that didn't work. I'm out of ideas and I don't even know where to begin, now.

Thank you in advance!
-Kevin

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 March 2006 - 05:26 PM

To strip any new line chars use this:
str_replace(array("\r", "\n"), "", $event[descrip])
It is very important that you use double quotes when dealing with whitespace chars otherwise PHP will treat them as normal characters if you are using single quotes.

#3 macdumbpling

macdumbpling
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 31 March 2006 - 07:35 PM

YAY! YAY! YAY! There was that and then the addslashes() that I needed to add to escape " ' " and now it works wonderfully!

Thank you so much, wildteen88!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users