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
https://forums.phpfreaks.com/topic/74552-too-many-backslashes/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.