提问人:Volatil3 提问时间:2/14/2023 更新时间:2/14/2023 访问量:34
使用 fgetcsv 替换数字中的逗号
Using fgetcsv to replace commans within numbers
问:
我有以下CSV数据:
"Symbol","Name","Trade Volume","Trade Value","UIN Settlement Volume ","UIN Settlement Value ","UIN Percentage Volume","UIN Percentage Value "
"786","786 INVESTMENT LIMITED","500.00"," 2,325.00 ","500.00"," 2,325.00 ","100.00 ","100.00 "
"ABL","ALLIED BANK LIMITED","15,501.00"," 981,819.00 ","10,001.00"," 629,379.00 ","64.52 ","64.10 "
"ABOT","ABBOTT LABORATORIES (PAKISTAN) LIMITED","4,043.00"," 1,730,197.34 ","3,031.00"," 1,299,214.65 ","74.97 ","75.09 "
"ACPL","ATTOCK CEMENT PAKISTAN LIMITED","40,231.00"," 2,345,598.00 ","36,131.00"," 2,107,058.00 ","89.81 ","89.83 "
"ADAMS","ADAM SUGAR MILLS LIMITED","4,091.00"," 107,544.61 ","3,091.00"," 80,669.61 ","75.56 ","75.01 "
在使用 拆分 w.r.t 逗号之前,我想从数字中删除命令。我该怎么做?fgetcsv
我当前的代码如下所示:
while (($row = fgetcsv($file, 1000, ",")) !== FALSE) {
print("<br><b>Before</b><br>");
print_r($row);
$row = preg_replace('/([0-9]+),([0-9]+)/', '$1$2', $row);
// $row = preg_replace('/"([0-9]+),([0-9]+)(.*?)"/', '"$1$2$3"', $row);
print("<br><b>After</b><br>");
print_r($row);
$Symbol = $row[0];
答:
0赞
Dimitry
2/14/2023
#1
是否要从所有单元格中的数字中删除多余的逗号和空格?
fgetcsv 的结果是一个数组,而不是一个字符串。您需要处理每个项目
while (($row = fgetcsv($file, 1000, ",", "\"")) !== FALSE) {
echo "<br><b>Before</b><br>" . PHP_EOL;
print_r($row);
$row = array_map(function($el) {
if (preg_match("/^[. ,0-9]+$/", $el)) {
$el = preg_replace("/,/", "", trim($el));
}
return $el;
},
$row);
echo "<br><b>After</b><br>" . PHP_EOL;
print_r($row);
$Symbol = $row[0];
}
评论
2,325.00
2
print_r($row);
2,325.00
double