Jump to content

[SOLVED] Format string to work inside Javascript


redbrad0

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.