drvkf88226 2017-11-12 16:39
浏览 54
已采纳

从一个文件中读取多个数据,PHP

I have this text file that I want to read and check if the value is correct or not and then display in html file:

Audi, 2006
BMW, 2019    //date incorrect
Toyota, 2016
Frd, 2017    //name incorrect 

All I did till now is:

$handle = file('src/can.txt');
$data = array();      
//loop to get the value from handle                                                
foreach ($handle as $key ) { 
  array_push($data, $key);
}

I wanted to continue using another loop where I create 2 array and then using explode method to separate the name of the car from the year of production.

My question is: is there any build in php method or any better way to perform the same operation?

  • 写回答

1条回答 默认 最新

  • douweihui0178 2017-11-12 17:19
    关注

    I would suggest to use method for repeating action. Here is an example and suggest you to handle the case-senitivity of brands/makes.

    I would also keep the array index for easy reference.

    <?php
        class File {
    
            private $_minYear = 2000,
                    $_maxYear = 2018,
                    $_brand = array('BMW','Audi','Toyota','Ford');
    
            private function validateYear($year) {
                $year = (int)$year;
                return ($this->_minYear < $year && $year < $this->_maxYear);
            }
    
            public function scan($file) {
                $lines = file($file);                                                  
                foreach ($lines as $key => $value) {
                    $data = explode(',',$value);
                    if (in_array($data[0], $this->_brand) && $this->validateYear($data[1])) {
                        $records[$key] = $value;
                    } else {
                        $errors[$key] = $value;
                    }
                }
                return array('records' => $records, 'errors' => $errors);
            }
        }
    
        $file = new File;
    
        $data = $file->scan('testcar.txt');
    
        echo '<pre>';
    
        print_r($data['records']);
    
        print_r($data['errors']);
    

    OUTPUT:

    Array
    (
        [0] => Audi, 2006
    
        [2] => Toyota, 2016
    
    )
    Array
    (
        [1] => BMW, 2019
    
        [3] => Frd, 2017
    )
    

    Without using Class/Method

    <?php
    
    $brands = array('BMW','Audi','Toyota','Ford');
    
    function validateYear($year) {
        $year = (int)$year;
        return (2000 < $year && $year < 2018);
    }
    
    function fileScan($file) {
        $lines = file($file);                                                  
        foreach ($lines as $key => $value) {
                $data = explode(',',$value);
                if (in_array($data[0], $brands) && validateYear($data[1])) {
                        $records[$key] = $value;
                } else {
                        $errors[$key] = $value;
                }
        }
        return array('records' => $records, 'errors' => $errors);
    }
    
    $data = fileScan('testcar.txt');
    
    echo '<pre>';
    
    print_r($data['records']);
    
    print_r($data['errors']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化