简体   繁体   English

php在csv输出文件中插入转义字符

[英]Php inserting escape characters in the csv output file

I am using this php code to allow the user to send dojo.enchancedgrid to file and download it to his machine. 我正在使用此php代码允许用户将dojo.enchancedgrid发送到文件并将其下载到他的计算机。 The procedure works fine, but the output csv file has the escape lines for the quotes. 该过程工作正常,但是输出的csv文件包含引号的转义行。 How can I avoid php inserting those for me. 我如何避免php为我插入那些。 The string that is passed to the function(str) is ok, but after passes through the php code is different. 传递给function(str)的字符串是可以的,但是经过php代码传递的字符串不同。

My javascript code: 我的JavaScript代码:

function exportCsv(){

var g = dijit.byId("grid");
g.exportGrid("csv",{
            writerArgs: {
                separator: ","
            }
            }, function(str){
                alert(str);


                    var form = document.createElement('form');
                    dojo.attr(form, 'method', 'POST');
                    document.body.appendChild(form);
                    dojo.io.iframe.send({
                            url: "csv.php",
                            form: form,
                            method: "POST",
                            content: {exp: str},
                            timeout: 15000
                    });
                    document.body.removeChild(form);

                    }); 

} }

My php code: 我的PHP代码:

$time = time();
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=\"grid_$time.csv\"");
$exportedData = $_POST['exp'];
echo $exportedData;

Also the server is inserting some analytics code in the rows as well… Thank you very much for the help guys. 服务器也在行中插入一些分析代码……非常感谢您的帮助。 Below is the output csv file: 以下是输出的csv文件:

Lon,Lat,Network,RouteId,Measure Lon,Lat,Network,RouteId,Measure

-74.18143812,40.62733114,\\"Named Route\\",IN278,1 -74.18143812,40.62733114,\\“命名的路线\\”,IN278,1

-73.90131905,40.75932358,\\"Named Route\\",IN278,25 -73.90131905,40.75932358,\\“命名的路线\\”,IN278,25

-73.8005193,40.73819062,\\"Named Route\\",IN495,10 -73.8005193,40.73819062,\\“命名路线”,IN495,10

-73.82437443,40.79381815,\\"Named Route\\",IN678,12 -73.82437443,40.79381815,\\“命名的路线\\”,IN678,12

-73.79087796,40.66589334,\\"Named Route\\",\\"NY 27\\",12 -73.79087796,40.66589334,\\“命名的路线\\”,\\“ NY 27 \\”,12

<-- Hosting24 Analytics Code --> <-Hosting24 Analytics代码->

<--End Of Analytics Code --> <-分析代码结尾->

  1. You can escape the quotes yourself on the client side string (Javascript) and unescape them in PHP - that way you control the whole process. 您可以自己在客户端字符串(Javascript)上对引号进行转义,并在PHP中取消转义-这样您就可以控制整个过程。
  2. The reason you have the hoster's "Analytics code" is that you're using free hosting (I assume). 您拥有托管人的“ Analytics代码”的原因是您使用的是免费托管(我认为)。 It's not analytics but ads, added to every http response. 它不是分析,而是广告,已添加到每个http响应中。 On an HTML page they look like ads at the bottom. 在HTML页面上,它们看起来像是底部的广告。 If you use AJAX, they just get in the way. 如果您使用AJAX,它们只会给您带来麻烦。 If you're actually paying for hosting, call your hoster and ask to him to get rid of those. 如果您实际上是为托管付费,请致电您的托管人并要求他摆脱那些托管人。

As zerkms suggested, this is likely due to magic quotes . 就像塞克姆斯建议的那样,这可能是由于magic quotes

Try disabling them. 尝试禁用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM