Jump to content

Too many backslashes


53329

Recommended Posts

My problem is this.  I have a php page generate a website in html.  The entire website is stored in one variable by the end of it.  At that point I use it to create a new page but at the same time I have to document.write it with javascript.

 

So I have to go from (as a much smaller example):

 

$template_merge="<div id=\"1\" name='alpha' onclick=\"myfunction(\\\"1\\\")\">My text</div>\n1";

 

Then I had to put it into an echo.  So I backslashed what I could.

 

$template_merge=str_replace("\\", "\\\\", $template_merge);

$template_merge=str_replace("\\\"", "\\\\\"", $template_merge);

$template_merge=str_replace("\"", "\\\"", $template_merge);

$template_merge=str_replace("\n", "\\n", $template_merge);

 

 

$newpage="<?php

echo \"" . $template_merge . "\";

?>";

 

 

So far so good (I think), but then I needed to modify the file so it acted as a temporary javascript file.  That means that the new page needs to echo a document.write.

 

$newpage="<?php

echo \"document.write(\\\"" . $template_merge . "\\\")\";

?>";

 

So the problem is that I don't know what to do to the variable $template_merge to make that last line work.  As is the first quote escapes the document.write, causes an error then , displays the rest of the code incorrectly.  I've been over it again and again but I keep getting lost in the backslashes when I try to replace them and I don't know how to do it.  Is there an easier way to do this or am I just missing something?

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.