php文件阅读显示不需要的内容

时间:2015-02-24 07:24:05

标签: php csv fgetcsv

我正在处理一些代码,我从stackoverflow和一些旧代码中拼凑了以下一些代码。无论如何,我试图让文件被解析和处理,而不是实际显示其内容,而是在后期进行。 有PHP:

$c = 0;
             if($fp = fopen($newname,"r"))
             { 
                 while (($line = fgets($fp)) !== false)
                 {
                          //CHECK STRING TO SEE IF IT CONTAINS AN EMAIL                         
                         $checkResult = $this->checkCSVContentForEmail($line,$c);//$content
                          if(!empty($checkResult))
                          {
                            if($checkResult == TRUE && is_bool($checkResult) == true)
                            {

                                   $this->contactsAddedCounter += 1;
                            }

                            elseif(is_string($checkResult))
                            {
                                $errLine .= '
                                 <p> CheckResult: '.$checkResult.' &nbsp; Line number: '.$c.' does not have an email</p>';
                                 array_push($this->LogError_contactsUpload, $errLine);  
                            }
                          }
                          else
                          {
                              $errLine = "<p>Line number: ".$c." contain's an empty string</p><br>\n";
                              array_push($this->LogError_contactsUpload, $errLine);
                          }

                    $c++;
                }
             }
             fclose($fp);

我昨天花了整整一天编写所有涉及的代码 - 函数和所有但不应该做任何显示,但我仍然会显示以下内容,

不需要的输出:

Array ( [0] => " [1] => [2] => [3] => [4] => Normal"" [5] => ""False"" [6] => [7] => [8] => ""Normal""";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ) 

当然,对于每行读取都会重复显示。

对可能导致此问题的任何帮助?

处理中涉及的其余代码:     //检查电子邮件方法行     公共函数checkCSVContentForEmail($ csvLine,$ lineNum)     {         如果(!空($ csvLine))         {
         $ partedCSV = explode(',',$ csvLine);

     if(!empty($partedCSV[57]))
     {
        $csvEmail = $partedCSV[57];
        if(!empty($csvEmail))
        {
             if($Result = $this->checkEmail($csvEmail, $lineNum))
             {
                //convert csv line into sql statement
                $csv2sqlInsertMultiLine = '(';
                $csvTosql = '';
                foreach($partedCSV as $csvContent)
                {
                    $str = strip_tags($csvContent);
                    $letters = array('"', "'");
                    $Quotes   = array('', ' ');
                    $strN  = str_replace($letters, $Quotes, $str);
                    $letters = array('(', ")");
                    $Quotes   = array('', '');
                    $c2s_str  = str_replace($letters, $Quotes, $strN);
                    $csvTosql[] .= $c2s_str;

                }
                    //(4,5,6);
                    $theSQL = implode(',',$csvTosql);
                    $csv2sqlInsertMultiLine .= $theSQL;
                    $csv2sqlInsertMultiLine .= '),';
                    array_push($this->csv2sqlInsertMultiLine, $csv2sqlInsertMultiLine);

                return $Result; 
             }
        }
     }
     else
     {
        $show = 'No Entry for Email address field - WILL NOT BE ADDED TO DATABASE! '.print_r($partedCSV); 
        return $show;
     }
    }
    else
    {
        $show = 'This line is empty';
        return $show;
    }
}
//Validate Email Method
public function checkEmail($email, $row)
{
    if(!empty($email))
    {
        $str = strip_tags($email);
        $letters = array('"', "'");
        $Quotes   = array('', ' ');
        $em  = str_replace($letters, $Quotes, $str);

        if(!empty($em) or $em !=' ')
        {
            $check = preg_match('/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU', $em) ? TRUE : FALSE;

             if($check == TRUE)
             {
                $this->contactEmail[$row] = $em;//array_push();                 
                return TRUE; 
             }
        }
        else
        {
            $msg = 'There is no email passed in: '.$em.' <br>Our check resulted in: '.$check;
            return $msg; 
        }
    }
     else
     {
         $msg = 'There is no email passed in: '.$email.' <br>Our check resulted in: '.$check;
        return $msg; 
     }
}

JQuery

// Upload Contacts
function uploadContacts()
{
    var obj = '#upImprtFile';

    var file_data = $k('#ContactsImprt').prop('files')[0];
    var form_data = new FormData();
    form_data.append("action", "uploadContacts");
    form_data.append("upImprtFile", "submit");
    form_data.append("ContactsImprt", file_data);

    $k.ajax({
                url: 'inc/runUtilities.php',
                dataType: 'text',
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,
                type: 'post',
                success: function(data)
                {
                    $k('#doUpload').html(data).fadeIn('slow');
                }
            }); 
}

1 个答案:

答案 0 :(得分:3)

请参阅checkCSVContentForEmail方法内的以下几行。

else
     {
        $show = 'No Entry for Email address field - WILL NOT BE ADDED TO DATABASE! '.print_r($partedCSV); 
        return $show;
     }

使用print_r必须导致输出。