Javascript和PHP5中的换行符

时间:2009-04-29 15:34:27

标签: php javascript xml csv newline

我有一个嵌入在XML文档中的CSV字符串,这对于使用XML的flash图表是必需的。问题是图表通过javascript片段从HTML文件中获取设置,如下所示:

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

所以,显然新行打破了javascript(由PHP变量回应)。无论如何都要删除换行符(或者至少停止换行符打破javascript),但仍然可以让flash图表正确地将其作为CSV文件读取。

2 个答案:

答案 0 :(得分:3)

试试这个。如果它不起作用,我有另一个想法。

<?php

// assumes *nix style newlines
echo str_replace( "\n", "\\n", $jsCode );

答案 1 :(得分:0)

stringObj.replace("\n","");

其中stringObj是

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