1

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?

Tronpora
  • 105
  • 1
  • 1
  • 8

1 Answers1

0

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']);
Michael Eugene Yuen
  • 2,470
  • 2
  • 17
  • 19
  • Thank you for you time but the solution above is quite complicate for me as I am at the beginning. Is there any other way to do it, assuming that I only know the syntax of the file and not the content. – Tronpora Nov 12 '17 at 17:59
  • I have edited the answer and using php functions instead of class/method. Would that be easier for you to understand? Just try it out and find out how in_array() works. Others should be quite self-explanatory :) – Michael Eugene Yuen Nov 12 '17 at 18:06
  • Thank you for you answer, really appreciated!, got my answer – Tronpora Nov 12 '17 at 18:30