How to extract data from csv file in PHP

前端 未结 11 1456
醉酒成梦
醉酒成梦 2020-11-22 07:36

I have a csv file which looks like this

$lines[0] = \"text, with commas\", \"another text\", 123, \"text\",5;
$lines[1] = \"some without commas\", \"another          


        
11条回答
  •  自闭症患者
    2020-11-22 08:07

    I've built an application to extract data from a CSV file , this php application was used to show a daily quote for users.

    The full project on github: 365-quotes-php-csv.

    Also this is the class Code for the application i've built

      readCsvAndGetQuote($dayCount)){
                    return $this->getQuote();
                }else{
                    echo 'Error Cannot open the .CSV File';
                }
            }
    
    
    
    
    
        //Methods
    
        //get Instance
        public function getInstance(){
                if(!isset(self::$_instance)){
                    self::$_instance = new Quote();
                }
                return self::$_instance;
            }//end of get Instance
    
    
    
    
        //get daily Quote   
        public function getQuote(){
                return $this->_quote;
            }//end of get Quote
    
    
    
    
        //Read CSV
        private function readCsvAndGetQuote($dayCount = 1 ){
    
            if(($handel = fopen("csv/all.csv" , "r")) !== false){
                $this->_allQuotes = fgetcsv($handel,1000000,';');
                $this->_quote = explode(',',$this->_allQuotes[$dayCount]);
                return true;
            }
            return false;
    
        }//end of read CSV
    
    
    
    }//end of Class
    

提交回复
热议问题