剥离" $"来自csv文件

时间:2015-06-29 16:33:30

标签: php csv

我有一个php页面,它接收一个csv文件并将其上传到我的数据库。我遇到的问题是,某些字段中有一个' $'我不需要。有没有办法从一些字段中删除$?

数据结构

COLL_ID   COCC_ID   AGEDTOTAL    PAYMENTTOTAL    
5074      11110     $7.50        ($7.50)

我的查询

$deleterecords = "TRUNCATE TABLE mytable"; //empty the table of its     current records
mysql_query($deleterecords);

//Upload File
if (isset($_POST['submit'])) {
    $i=0;
    if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
    echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
    echo "<h2>Displaying contents:</h2>";
    readfile($_FILES['filename']['tmp_name']);
}

//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    if ($i>0){
    $import="INSERT into mytable(COLL_ID, COCC_ID, AGEDTOTAL, PAYMENTTOTAL) values('$data[0]','$data[1]','$data[2]','$data[3]')";

    mysql_query($import) or die(mysql_error());
}
$i++;
}

1 个答案:

答案 0 :(得分:0)

对于整个文件

$string = preg_replace(
    '/((:?.*?\,){2}.*?)\$/',
    '$1',
    $string
);

2是((包含$符号的列数) - 1)

$data[2] = str_replace('$', '', $data[2]);