redbrad0 Posted January 8, 2009 Share Posted January 8, 2009 I have a function that returns information and either displays it in HTML on the page, or displays it in Javascript. The problem is if someone puts content with line breaks, that messes up Javascript. The problem is that when it writes it out into the browsers html, it doesnt display the \n, it just does a character return. How can I stop this? function get_ImportantEventInformation($DisplayType='HTML') { if ($DisplayType=='Javascript') { return str_replace("\r\n", "\n", $this->options['ImportantEventInformation']); } else { return nl2br($this->options['ImportantEventInformation']); } } Link to comment https://forums.phpfreaks.com/topic/140086-solved-format-string-to-work-inside-javascript/ Share on other sites More sharing options...
btherl Posted January 9, 2009 Share Posted January 9, 2009 I'm not clear on what your problem is. What is the exact output you want? And how does the javascript display the string? Link to comment https://forums.phpfreaks.com/topic/140086-solved-format-string-to-work-inside-javascript/#findComment-732993 Share on other sites More sharing options...
redbrad0 Posted January 9, 2009 Author Share Posted January 9, 2009 var purchase_warning="**** EVENT INFORMATION - CONFIRM WITH CUSTOMER ****\n\n"; purchase_warning+= "- Must be at the venue by 9:30 PM. - Under 18 must have a adult"; purchase_warning+="\n\nCall (866) 888-8888 if you have any questions."; Sorry I wasnt that clear. Starting Code var purchase_warning="**** EVENT INFORMATION - CONFIRM WITH CUSTOMER ****\n\n"; purchase_warning+= "{$Event->get_ImportantEventInformation('Javascript')}"; purchase_warning+="\n\nCall (866) 888-8888 if you have any questions."; Output var purchase_warning="**** EVENT INFORMATION - CONFIRM WITH CUSTOMER ****\n\n"; purchase_warning+= "- Must be at the venue by 9:30 PM. - Under 18 must have a adult"; purchase_warning+="\n\nCall (866) 888-8888 if you have any questions."; Expected Output var purchase_warning="**** EVENT INFORMATION - CONFIRM WITH CUSTOMER ****\n\n"; purchase_warning+= "- Must be at the venue by 9:30 PM.\n - Under 18 must have a adult"; purchase_warning+="\n\nCall (866) 888-8888 if you have any questions."; Link to comment https://forums.phpfreaks.com/topic/140086-solved-format-string-to-work-inside-javascript/#findComment-732994 Share on other sites More sharing options...
btherl Posted January 9, 2009 Share Posted January 9, 2009 Aha. Try using single quotes (') instead of double quotes (") in php. That should pass the \n through unaltered, where javascript can change it into a newline. return str_replace("\r\n", '\n', $this->options['ImportantEventInformation']); The reason being that "\n" in php is translated into an actual newline characters. But '\n' is not processed at all by php. Link to comment https://forums.phpfreaks.com/topic/140086-solved-format-string-to-work-inside-javascript/#findComment-732996 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.