如何从PHP中的csv删除引号 [英] how to remove quotes from csv in php

查看:36
本文介绍了如何从PHP中的csv删除引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从DB获得的数组.在此项目中,即时将我的数组转换为csv文件.但是,每次我打开文件时,我都会得到两倍的资产.我尝试了str_replace和preg_place,但没有成功.如何删除引号

I have a array that i am getting from DB. In this project, im converting my array to csv file. But every time i open the file i get double quoetes. I have tried with str_replace and preg_place with no succes. How can i remove quotes

这是我的csv代码

$query = "SELECT t.transactiontime, t.restaurant_id, t.transactionid, t.cardid, emd.m_field_id_2, t.pricebefordiscount, t.menucard_cut
from transactions as t
left join exp_member_data AS emd ON (t.cardid-10000000 = emd.member_id) order by t.transactiontime desc limit 50";

$transactions_query = ee()->db->query($query);
$transactions_result = $transactions_query->result_array();

$transaction_array = array();
foreach ($transactions_result as $key) 
{
  $date = new DateTime($key['transactiontime']);
  $newdate = $date->format('d.m.Y');


 $transaction_array[] = array(
    'transactiontime' => $newdate,
    'restaurant_id' =>  $key['restaurant_id'], 
    'member' => $key['transactionid'] . " " . $key['m_field_id_2'],
    'pricebefordiscount' => $key['pricebefordiscount']/100,
    'menucard_cut' => $key['menucard_cut']
    ); 


}


function outputCSV($data) 
    {

$outstream = fopen("php://output", 'w');



function __outputCSV(&$vals, $key, $filehandler) 
{
    fputcsv($filehandler, $vals, ';');
}

array_walk($data, '__outputCSV', $outstream);

fclose($outstream);
}

outputCSV($transaction_array);

我的输出

19.08.2013;47657;"12459 Abdullahi";60;
19.08.2013;47658;"12455 atima";30;

推荐答案

引号确实没有错.它们避免了某些CSV使用空格作为分隔符时可能发生的任何混乱:

There really is nothing wrong with the quotes. They avoid any confusion that might occur when some CSV's use whitespace as delimiter:

data    "some more"    another thing
//is not the same as:
data    some more    another thing

但是,如果要删除它们,请将此正则表达式应用于每行:

However, if you want to remove them, apply this regex to each line:

$line = preg_replace('/(^|;)"([^"]+)";/','$1$2;',$line);

你应该没事的.
运作方式:

And you should be all right.
How does it work:

  • (^ |;)匹配(并捕获)行的开头或分号
  • "与文字" (不捕获)匹配
  • ([[^] +):匹配并捕获至少一个 not "
  • 的字符
  • ; :匹配(不捕获)文字" ;
  • $ 1 $ 2; : $ 1 是对第一个匹配组((^ |;))
    $ 2 引用([^^;] +); 只是一个文字
  • (^|;) matches (and captures) either the beginning of a line, or a semi-colon
  • " matches a literal " (doesn't capture)
  • ([^" ]+): matches and captures at least one char that is not "
  • ";: matches (no capture) a literal " and ;
  • $1$2;: the $1 is a back-reference to the first matched group ((^|;))
    The $2 references ([^";]+), the ; is just a literal

假设 $ line '19 .08.2013; 47657;"12459 Abdullahi"; 60;',即结果(在 preg_replace 之后呼叫)为: '19 .08.2013; 47657; 12459 Abdullahi; 60;'.引号不见了.

Suppose $line is '19.08.2013;47657;"12459 Abdullahi";60;', the result (after the preg_replace call) would be: '19.08.2013;47657;12459 Abdullahi;60;'. The quotes are gone.

但是,如果某些字段包含一个" char,则可能会对其进行转义( \" ),以防止正则表达式无法发现该字符,这是一个使用前瞻性断言的代码:

However, if some field were to contain a " char, it'll probably get escaped (\"), so to prevent the regex from failing to spot that, here's one that uses a lookahead assertion:

$line = preg_replace('/(?<=^|;)"(.+)"(?=;)/','$1',$line);

区别:

  • (?< = ^ |;)难以捕捉.模式中的下一项仅在其开头是字符串( ^ )或分号
  • 的情况下才匹配
  • (.+)现在是第二组.它匹配所有内容,包括" BUT:
  • (?=;)仅在其后跟; 的情况下与" 匹配.
  • (?<=^|;) a non-capturing positive lookbehind. The next thing in the pattern will only match if it's preceded either by the beginning of the string (^) or a semi-colon
  • (.+) is now the second group. It matches everything, including " BUT:
  • "(?=;) this matches a " only if it's followed by a ;.

当出现类似 '19 .08.2013; 47657;"12459 \" Abdullahi \"; 60;'的行时,后一个表达式将返回 19.08.2013; 47657;12459 \"Abdullahi \"; 60; <-仅删除了未转义的引号

When presented with a line like '19.08.2013;47657;"12459 \"Abdullahi\"";60;', the latter expression will return 19.08.2013;47657;12459 \"Abdullahi\";60; <-- it only removed the quotes that weren't escaped

这篇关于如何从PHP中的csv删除引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆