Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generate JavaScript friendly string using PHP

Status
Not open for further replies.

Stickbread

Programmer
Mar 12, 2005
3
US
After I download the page, I use PHP's addslashes method to every line, and then I just wrap every line in a document.write. Here is the script (script.php):
Code:
$url = $_GET["url"];
$file = file("$url");
for($i = 0; $i < count($file); $i++)
{
     $line = $file[$i];
     $line = substr($line, 0, strlen($line)-1); // removes the new line character
     $line = addslashes($line);
     $str = "document.write(\"$line\");\n";
     print($str);
}

Then to display a webpage, I use:
<script language="JavaScript" src="script.php?url=http://www.google.com"></script>

However, this is not working on all web pages. For example, it works for but not for and most others. My guess is that PHPs addslashes does not escape the proper characters to generate a javascript friendly string.

Could somebody help me get this script working? It's rather urgent.
 
i think this may be a new line issue. another post of yesterday addressed it.

i recall that the solution was to replace the "\r" and "\n" characters with a blank. this may, of course, have a detrimental effect on the formatting.
Code:
$line = str_replace(array("\n","\r"),array("",""),$line);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top