views:

246

answers:

2

Hi, I have a CSV string that is embedded within an XML document, which is necessary for a flash chart which uses XML. The problem is that the chart gets the settings from the HTML file through a javascript snippet, like so:

<script type="text/javascript">
     // <![CDATA[  
     var so = new SWFObject("/gr/amstock.swf", "line", "100%", "100%", "8", "#FFFFFF");
     //so.addVariable("path", "../amline/");
     so.addVariable("chart_settings", escape('<settings><number_format><thousands_separator></thousands_separator><decimals_separator><![CDATA[.]]></decimals_separator></number_format><number_format><thousands_separator></thousands_separator><decimals_separator><![CDATA[.]]></decimals_separator></number_format><redraw><![CDATA[true]]></redraw><data_sets><data_set did="0"><color><![CDATA[#00688B]]></color><csv><separator><![CDATA[,]]></separator><date_format><![CDATA[MM/DD/YYYY]]></date_format><data><![CDATA[03/29/09,0
03/30/09,0
03/31/09,0
04/01/09,0
04/02/09,0</data><columns>...

So, evidently the newlines break the javascript (which is being echoed by a PHP variable). Is there anyway to remove the newlines (or at least stop the newlines breaking the javascript) and yet still get the flash chart to correctly read it as a CSV file.

thanks in advance.

+3  A: 

Try this. If it doesn't work, I have another idea.

<?php

// assumes *nix style newlines
echo str_replace( "\n", "\\n", $jsCode );
Peter Bailey
Thanks, that worked fine.
Ian
A: 
stringObj.replace("\n","");

where stringObj is

<script type="text/javascript">
        // <![CDATA[            
        var so = new SWFObject("/gr/amstock.swf", "line", "100%", "100%", "8", "#FFFFFF");
        //so.addVariable("path", "../amline/");
        so.addVariable("chart_settings", escape('<settings><number_format><thousands_separator></thousands_separator><decimals_separator><![CDATA[.]]></decimals_separator></number_format><number_format><thousands_separator></thousands_separator><decimals_separator><![CDATA[.]]></decimals_separator></number_format><redraw><![CDATA[true]]></redraw><data_sets><data_set did="0"><color><![CDATA[#00688B]]></color><csv><separator><![CDATA[,]]></separator><date_format><![CDATA[MM/DD/YYYY]]></date_format><data><![CDATA[03/29/09,0
03/30/09,0
03/31/09,0
04/01/09,0
04/02/09,0</data><columns>...
Andrew Sledge