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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料