Question

I have a text file, data.txt, which contains the words in "quotation marks" and separated by comma like this:

"e_mail","id_name","city","name","firstname","how","date" "my@exemple.com","125","NY","Wells","tommy","","20130101" "hisgoodemail@exemple.com","132","NY","Waits","John","","20120101" "hisbadmailxemple.com","325","CH","Wells","Don","","20130101" "goodmail3@exemple.com","222","NY","Wools","John","","20140101"

I will remove bad record/registration with the criterion of email, in my exemple it is 4. registration/record.

So I check the email format and I delete the wrong records which contains wrong format email.

After I would like put the new data in another file TEXT with the words in "quotation marks" and separated by comma like this:

"e_mail","id_name","city","name","firstname","how","date" "my@exemple.com","125","NY","Wells","tommy","","20130101" "hisgoodemail@exemple.com","132","NY","Waits","John","","20120101" "goodmail3@exemple.com","222","NY","Wools","John","","20140101"

But when I put the new data in the new file, its missing the "quotation marks" and comma :

    $fileData = 'data.txt';
if (($handle = fopen($fileData, "r")) !== FALSE) 
{
fgetcsv($handle, 0, ',');

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
    {
        $arrayContainsFileData = array
        (
            'e_mail' => $data[0],
            'id_name' => $data[1],
            'city' => $data[2],
            'name' => $data[3],
            'firstname' => $data[4],
            'how' => $data[5],
            'date' => $data[6]
        );
        //      var_dump($arrayContainsFileData);
        //        exit();

        $e_mail = $arrayContainsFileData['e_mail'];
        $e_mail = str_replace(' ', '', $e_mail);

        //remove bad record/registration with the criterion of email
        if(!VerifyEmailFormat($e_mail))
        { //echo 'bad format'';
            unset($arrayContainsFileData['e_mail']);
            unset($arrayContainsFileData['id_name']);
            unset($arrayContainsFileData['city']);
            unset($arrayContainsFileData['name']);
            unset($arrayContainsFileData['firstname']);
            unset($arrayContainsFileData['how']);
            unset($arrayContainsFileData['date']);
        }

        //          var_dump($arrayContainsFileData);
        //        exit();   

        $creationNewData = 'newCleanData' . date('Y_m_d__H_i_s__u'). '.txt';
        $openNewData = fopen($creationNewData, "a+");
        foreach ($arrayContainsFileData as $value)
            {
            fputs($openNewData, $value.' ');
            }
        fclose($openNewData);
    }
}
//__halt_compiler

how should I do putting the words in "quotation marks" and separated by comma ?

Was it helpful?

Solution

Instead of looping through each element of your array, you could use the php implode function (http://ca3.php.net/implode) to turn each array into a string first.

$str = '"' . implode('","', $arrayContainsFileData) . '"';
fputs($openNewData, $str);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top