代码之家  ›  专栏  ›  技术社区  ›  Geraldo Isaaks

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

  •  1
  • Geraldo Isaaks  · 技术社区  · 11 年前

    我正在编写一些代码,我从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) { if(!空($csvLine)) {
    $partedCSV=爆炸(',',$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 回复  |  直到 11 年前
        1
  •  3
  •   Vivek Vaghela    11 年前

    请参见内部的以下行 checkCSVContentForEmail 方法

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

    使用 print_r 必须导致输出。