删除'来自PHP CSV导入的撇号

时间:2016-09-09 11:42:29

标签: php csv import fopen fgetcsv

所以我有点超出我的深度(再次),并且需要在将CSV数据集导入我的数据库时从CSV数据集中删除任何撇号...

我现在的代码如下所示

<?php

include "connection.php"; //Connect to Database

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

//Upload File
if (isset($_POST['submit'])) {
    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) {
        $import="INSERT into riders_long(Onlineentry,SaleID,FirstName,LastName,ClubTeamName,DOB,IsBCMember,MemberNo,Agreetermsandconditions,Agreeemailconfirmation,Agreeemailmarketing,LicenceCategory,Gender,Email,Daytimephone,Address1,Address2,TownCity,Region,Country,Postcode,EmergencyContactName,EmergencyContactPhoneNumber,DatePaid,AmountPaid,RefundAmount,PaymentType,Status) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]','$data[12]','$data[13]','$data[14]','$data[15]','$data[16]','$data[17]','$data[18]','$data[19]','$data[20]','$data[21]','$data[22]','$data[23]','$data[24]','$data[25]','$data[26]','$data[27]')";

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

    fclose($handle);

    print "Import done";

    //view upload form
}else {

    print "Upload new csv by browsing to file and clicking on Upload<br />\n";

    print "<form enctype='multipart/form-data' action='upload.php' method='post'>";

    print "File name to import:<br />\n";

    print "<input size='50' type='file' name='filename'><br />\n";

    print "<input type='submit' name='submit' value='Upload'></form>";

}

?>

如果我说实话,我甚至不知道从这个地方开始......

我认为我需要使用str_replace()函数......

中的某些内容
$remove = array("'");
$filtered = str_replace($remove, "", "Hello World of PHP");
然后

$filtered将成为没有&#39;撇号的字符串,但后来我对如何以及在哪里最好地整合此

感到困惑

非常感谢建议从哪里开始

1 个答案:

答案 0 :(得分:1)

使用can me made infix,就像这样:

$data = file_get_contents(...);
$cleanData = str_replace("'","",$data);