PDA

View Full Version : PHP unwnted linebreak's in fwrite....coincides with JS long string bug.


singedpiper
05-18-2005, 05:44 AM
so, i am, for unknowably complicated reasons, using php to generate a .js file that contains a MASSIVE string literal that I obtain from a form and write to the file. Due to the JS long string bug (whereby any string that spans multiple lines in a file creates an error) I took my huge php string and removed all instances of "\n" and added "\"\n+\"" every hundred characters... however, when i fwrite the string, i get newlines in places other than they should be. I'm on windows if that is a factor (i.e. \n\r vs. \n).


here's the php in question... summary and artical are the huge strings:

<?php
include("protect.php");
$filename = "newsData.js";

$date = addslashes(nl2br($_POST[date]));
$title = nl2br($_POST[title]);
$summary = nl2br($_POST[summary]);
$artical = nl2br($_POST[artical]);

$bugfix = "\"\n+\"";

$date = str_replace("\n","",$date);
$title = str_replace("\n","",$title);
$summary = str_replace("\n","",$summary);
$artical = str_replace("\n","",$artical);


if(strlen($summary)>100)
for($i=100;$i<strlen($summary);$i+=104)
{
$summary = substr_replace($summary, $bugfix, $i, 0);
}
if(strlen($artical)>100)
for($i=100;$i<strlen($artical);$i+=104)
{
$artical = substr_replace($artical, $bugfix, $i, 0);
}






//newsData[newsData.length] = date ;
$line1 = "newsData[newsData.length] = \" " . ($date)."\";\n";
$line2 = "newsData[newsData.length] = \" " . ($title) . "\";\n";
$line3 = "newsData[newsData.length] = \" " . ($summary) . "\";\n";
$line4 = "newsData[newsData.length] = \" " . ($artical) . "\";\n";

$content = $line1.$line2.$line3.$line4;

if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $content) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote:\n\n ($content) \n to file: \n ($filename)";

fclose($handle);


?>



here's the output I need:

newsData[newsData.length] = " 05/18/05";
newsData[newsData.length] = " Debut of GrahamHighlanders.com";
newsData[newsData.length] = " The newly redesigned website of the Graham Highlanders is now online.";
newsData[newsData.length] = " GrahamHighlanders.com has been a project several months in the making, and several years in the plan"
+"ning. The design is the creation of Bill Herreid, Community Piper, and the code was written by Greg"
+" Schoppe, alumnus. They have spent much of their free time over the last semester finalizing plans,"
+" designing layouts and tables, battling glitches, and finally finding hosting. The Graham <br />Hi"
+"ghlanders thank them for their dedication.<br /><br />The site contains several new and improved f"
+"eatures, such as dynamic content on many pages, enabling the schedule, news, gallery, and other page"
+"s to be updated frequently. it also features a fully redesigned store, <br />offering high quality"
+" promotional products. For the band\\\'s benefactors we have created a sponsors page, where individ"
+"uals, companies, or organizations who donate to furthering our cause can have a half banner link <br"
+" />to show their support for the Celtic arts in Vermont.<br /><br />This new site should serve us"
+" well for the forseeable future, so enjoy your visit and return soon.";



here's what I get:

newsData[newsData.length] = " 05/18/05";
newsData[newsData.length] = " Debut of GrahamHighlanders.com";
newsData[newsData.length] = " The newly redesigned website of the Graham Highlanders is now online.";
newsData[newsData.length] = " GrahamHighlanders.com has been a project several months in the making, and several years in the plan"
+"ning. The design is the creation of Bill Herreid, Community Piper, and the code was written by Greg"
+" Schoppe, alumnus. They have spent much of their free time over the last semester finalizing plans,"
+" designing layouts and tables, battling glitches, and finally finding hosting. The Graham <br />
Hi"
+"ghlanders thank them for their dedication.<br />
<br />
The site contains several new and improved f"
+"eatures, such as dynamic content on many pages, enabling the schedule, news, gallery, and other page"
+"s to be updated frequently. it also features a fully redesigned store, <br />
offering high quality"
+" promotional products. For the band\\\'s benefactors we have created a sponsors page, where individ"
+"uals, companies, or organizations who donate to furthering our cause can have a half banner link <br"
+" />
to show their support for the Celtic arts in Vermont.<br />
<br />
This new site should serve us"
+" well for the forseeable future, so enjoy your visit and return soon.";

singedpiper
05-18-2005, 05:51 AM
well, i just checked, and removing "\r"s seems to have done it. that, i think, may qualify as the strangest bug I've ever seen.

thanks for reading.