我们有一个PHP脚本,可以将订单导出到.csv文件。我们导出的系统也需要将每个字段封装在引号中。
以下是我们设置每个字段的代码。
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<title>Central de Controle da Engenharia</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/redmond/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js">
</script>
</head>
<body>
<div id="div_main">
<header id="header" style="text-align:center;">Central de Controle da Engenharia</header>
<section id="content">
<header id="navbar">
<input type="text" id="vpad-pesq">
<select>
<option>Código</option>
<option>Nome</option>
</select>
<div id="navbutton">
<button id="btfirst"></button>
<button id="btprev"></button>
<button id="btnext"></button>
<button id="btlast"></button>
</div>
</header>
<table id="table_model">
<colgroup>
<td class="colgroup1">Código</td>
<td class="colgroup1">Descrição</td>
<td id="black_corner"></td>
</colgroup>
</table>
<div id="navdata" role="main">
<table style="overflow:auto">
<tbody id="tbody_test">
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>002</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr class="color"><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
<tr><td>001</td><td>Coxa c/ sobrecoxa s/ osso s/ pele</td></tr>
</tbody>
</table>
</div>
</section>
<section id="msgbar"></section>
<footer id="footer">Rodapé</footer>
</div>
</body>
</html>
这输出;
H,HOME,&#34; 2015-05-13 13:19:46&#34;,REAL ,,,,,&#34; Ben Bull&#34;,&#34;地址1地址2&#34 ;,
有些是围绕&#34;&#34;还有一些人不知道他们是怎么做到的?
答案 0 :(得分:0)
对于CSV输出,您需要用双引号括起所有值。此外,如果值中有双引号,则需要使用两个连续的双引号来转义这些双引号。这就是CSV的工作原理。
检查下面的PHP函数。
function makeCSV($value) {
//Encloses each token (Before and after)
$CSV_TOKEN_ENCLOSER = '"';
//Used to escape the enclosing character if inside the token
$CSV_TOKEN_ENCLOSER_ESCAPER = '""';
//Escape the encloser inside the value
$csv_value = str_replace($CSV_TOKEN_ENCLOSER, $CSV_TOKEN_ENCLOSER_ESCAPER, $value);
//Enclose the value
$csv_value .= $CSV_TOKEN_ENCLOSER . $csv_value . $CSV_TOKEN_ENCLOSER;
//Return
return $csv_value;
}
这完成了我在第一段中解释的工作。你可以在你的情况下使用它:
$order_data = array(
'type' => makeCSV("H"),
'order_type' => makeCSV('HOME'),
'order_date' => makeCSV($order->order_date),
...
);
但是,看起来您的代码会自动为您的订单对象中的值包含引号。我建议你避免使用上面提到的makeCSV函数的代码,然后最后使用标准的PHP内部调用来获取你的CSV:
$comma_separated_csv = implode(",", $order_data);
希望这有帮助。
干杯。
答案 1 :(得分:-1)
尝试将所有类型强制为字符串:
'order_type' => (string) 'HOME'