?
Solved

Read file lines and assign them to variables

Posted on 2011-03-10
9
Medium Priority
?
399 Views
Last Modified: 2012-05-11
Hi Experts,

I wanna read from text file, the text file look like this:
test.txt:
----start file ----
1
2
3
4
---
1
6
2
8
---
9
8
2
4
----end file ----

As you have seen, the text file starts by four lines and then a separator (---). What I want is that, I want to read the text file from the first line to the first separator (---) and then assign each line ( there are four lines) and then print them like this:
from first line to the first separator:
for (first line to the first separator ) {
echo var[0];  //Var[0] = 1
echo var[1];  //Var[1] = 2
echo var[2];  //Var[2] = 3
echo var[3];  //Var[3] = 4
}
----------------
from after the first separator to the second separator:
for (after the first separator to the second separator ) {
echo var[0];  //Var[0] = 1
echo var[1];  //Var[1] = 6
echo var[2];  //Var[2] = 2
echo var[3];  //Var[3] = 8
}
----------------
from after the second separator to the third separator:
for (after the second separator to the third separator ) {
echo var[0];  //Var[0] = 9
echo var[1];  //Var[1] = 8
echo var[2];  //Var[2] = 2
echo var[3];  //Var[3] = 4
}

I am sure we can do one for loop for all the lines, can you help me with this?

Thanks in advance.
Regards
0
Comment
Question by:Sat80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 4

Expert Comment

by:Doug Walton
ID: 35098187
Maybe something like this:

$fp = fopen("filepath", "r");
$numbers = array();
$i = 0;
while ($line = fgets(file))
{
  if($line != "---")
  {
    $numbers[$i] = $line;
    echo $line;
  }
  else //you could do something with the variables you just set in this else
    $i = 0;
  $i++;
}

Open in new window


You could take out the whole $numbers stuff if all you want to do is print (I put it in because it seemed like you wanted an array).
0
 

Author Comment

by:Sat80
ID: 35098319
Hi,
Thanks for help.
The variable (file) is undefined? do you mean the path of txt file?

Yes you are right, actually I will insert them to DB so when they are in array then I can insert each line to different column
Thanks
0
 
LVL 4

Expert Comment

by:Doug Walton
ID: 35098459
Oops sorry I meant $fp not file

$fp = fopen("filepath", "r");
//connect to db
$numbers = array();
$i = 0;
while ($line = fgets($fp))
{
  if($line != "---")
  {
    $numbers[$i] = $line;
    echo $line;
  }
  else //you could do something with the variables you just set in this else
  {
    //insert into....
    $i = 0;
  }
  $i++;
}
0
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 

Author Comment

by:Sat80
ID: 35098702
Hi,

The code is reading the lines and output them, but I wanna have something like this:

$numbers[0] = first line
$numbers[1] = second line
$numbers[2] = third line
$numbers[3] = fourth line

and then read the second value after the separator (---) and then again the same array with the four values until read the whole value? I know that there will be four lines and then the separator (---) in the whole file as I have shown in the question. Can you please help me with this?
Thx
0
 

Author Comment

by:Sat80
ID: 35098891
This should work , in theory, but it didn't work when try it, Can you please help me with it?


$fp = fopen("test.txt", "r");
$numbers = array();
$i = 0;
while ($line = fgets($fp))
{
  if($line != "---")
  {
    $numbers[$i] = $line;
    echo $line;

  }
  else //you could do something with the variables you just set in this else
  {
    //insert into....
   
      echo  $numbers[0];
      echo "<br>";
      echo  $numbers[1];
      echo "<br>";
      echo  $numbers[2];
      echo "<br>";
      echo  $numbers[3];
      echo "<br>";
      echo  $numbers[4];
      reset($numbers);
      $i = 0;
      echo "<br>---<br>";
  }
  $i++;
        
}
0
 
LVL 4

Expert Comment

by:Doug Walton
ID: 35099053
I think this will work... I moved the $i++ into the if statement.  It was skipping $numbers[0] each time after the first one.  Was anything else not working about it?
$fp = fopen("test.txt", "r");
$numbers = array();
$i = 0;
while ($line = fgets($fp))
{
  if($line != "---")
  {
    $numbers[$i] = $line;
    echo $line;
    $i++;
  }
  else //you could do something with the variables you just set in this else
  {
    //insert into....
   
      echo  $numbers[0];
      echo "<br>";
      echo  $numbers[1];
      echo "<br>";
      echo  $numbers[2];
      echo "<br>";
      echo  $numbers[3];
      echo "<br>";
      echo  $numbers[4];
      reset($numbers);
      $i = 0;
      echo "<br>---<br>";
  }   
}

Open in new window


Also if you're only taking in 4 numbers, you shouldn't need $numbers[4]
0
 

Author Comment

by:Sat80
ID: 35099100
Hi, Thanks for helping me, I guess there is something wrong with the array, as I have deleted
echo $line;
So it will only print the array number when the line is (---), but will find a blank page? what do you think? This is the code I am using:
<?PHP
$fp = fopen("test.txt", "r");
$numbers = array();
$i = 0;
while ($line = fgets($fp))
{
  if($line != "---")
  {
    $numbers[$i] = $line;

    $i++;
  }
  else //you could do something with the variables you just set in this else
  {
    //insert into....
   
      echo  $numbers[0];
      echo "<br>";
      echo  $numbers[1];
      echo "<br>";
      echo  $numbers[2];
      echo "<br>";
      echo  $numbers[3];
      echo "<br>";
      echo  $numbers[4];
	  
      reset($numbers);
      $i = 0;
      echo "<br>---<br>";
  }   
}
?>

Open in new window

0
 
LVL 4

Accepted Solution

by:
Doug Walton earned 2000 total points
ID: 35099249
OK, this works for me:
<?php
$fp = fopen("test.txt", "r");
$numbers = array();
$i = 0;
while ($line = fgets($fp))
{
  $line = trim($line);
  if($line != "---")
  {
    $numbers[$i] = $line;
    $i++;
  }
  else
  {
   
      echo  $numbers[0];
      echo "<br>";
      echo  $numbers[1];
      echo "<br>";
      echo  $numbers[2];
      echo "<br>";
      echo  $numbers[3];
      echo "<br>";
	  
      reset($numbers);
      $i = 0;
      echo "<br>---<br>";
  }
}
echo  $numbers[0];
echo "<br>";
echo  $numbers[1];
echo "<br>";
echo  $numbers[2];
echo "<br>";
echo  $numbers[3];
echo "<br>";
?>

Open in new window


Hope that helps!
0
 

Author Comment

by:Sat80
ID: 35099276
Yes, its working now, thanks :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question