text - Reading .txt files with PHP and store information into variables

213

I have a two.txt files that I need to read. The content of the two files is below

log.txt

2013/04/20 01:08:11

and

data.txt

MANUFACTURER: SISYSTEM
MODELNAME: DX1
BIRTHDATE: 19710108
SEX: M

For log.txt i need to store the date from the first line and save it into mySQL database.

For data.txt i need to store information from each line into variables. i.e:

$manufacturer = SISYSTEM;
$modelname = DX1;
$birthdate =  19710108;
$sex = M;

For log.txt i can simply just read the first line using fget and store into one variable. However, for data.text, I am having trouble figuring out how to store multiple variables inside the loop. Here's what I have so far, but I don't know how to assign the values into new variables each time it goes through the while loop. Any help is appreciated!

<?PHP

$file_handle = fopen("data.txt", "r");

while (!feof($file_handle) ) {

   $line_of_text = fgets($file_handle);
   $parts = explode(':', trim($line_of_text) );


}

fclose($file_handle);

?>
27

Answer

Solution:

Why do you need them in variables? An associative array ("map") is the cleaner approach

$result = array();
  // Your loop -->
  $parts = array_map('trim', explode(':', $line_of_text, 2)));
  $result[$parts[0]] = $parts[1];
646

Answer

Solution:

Use variable variables: http://php.net/manual/en/language.variables.variable.php

Use in your while loop:

$line_of_text = fgets($file_handle);
$parts = explode(':', trim($line_of_text), 2);
${strtolower($parts[0])} = trim($parts[1]);
925

Answer

Solution:

Note: I don't know how to assign the values into new variables

You can use arrays.

I assume that you've just provided an example of what's in each file - that these patterns may repeat. Before I answer your question, I think it's important you should know that maintaining data in text files is a very bad idea or lots of reasons.

Taking the second file as an example, you've got 2 choices of how you manage this data in PHP - consider your data as a table:

manufctr  model   birthdate   sex
SISYSTEM  DX1     19710108    M
SISYSTEM  DX2     19710305    F
...

You could either store each row in an array, or each column in an array. And the resulting set of arrays can itself be stored in an array.

How you load and parse the data depends on how consistent the data structure will be (will each entry always be this format, or might it have extra / missing attributes). Whether you read the data into rows or columns depends on how the data is stored in the file and also what you intend doing next with the data.

$read=array();
while (true) {
   $record=array();
   for($x=0; $x<4; $x++) {
      $line_of_text = fgets($file_handle);
      list($k, $v) = explode(':', $line_of_text );
      $record[strtolower(trim($k))]=trim($v);
   }
   $read[]=$record;
   if (feof($file_handle) break;
}
442

Answer

Solution:

Inside thewhile loop you can compare the line to see what variable you have to assign: PHP: strcmp - Manual

<?php
$file_handle = fopen("data.txt", "r");

while (!feof($file_handle) ) {

  $line_of_text = fgets($file_handle);
  $parts = explode(':', trim($line_of_text) );
  if (strcmp($parts[0], "MANUFACTURER")==0)
      $MANUFACTURER = trim($parts[1]);
  if (strcmp($parts[0], "MODELNAME")==0)
     $MODELNAME = trim($parts[1]);
  [...]
  else
     echo "I haven't recognised that: ".$parts[0];
}

fclose($file_handle);
?>

Probably you could use aswitch statement

<?php
$file_handle = fopen("data.txt", "r");

while (!feof($file_handle) ) {

  $line_of_text = fgets($file_handle);
  $parts = explode(':', trim($line_of_text) );
  switch($parts[0])
  case 'MANUFACTUREER':
      $manufactureer = $parts[1];
      break;
  case 'MODELNAME':
      $modelname = $parts[1];
      break;
  [...]
}

fclose($file_handle);
?>

People are also looking for solutions to the problem: php - Output of the table in div class

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.