Solved

Read file lines and assign them to variables

Posted on 2011-03-10
9
385 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
  • 5
  • 4
9 Comments
 
LVL 4

Expert Comment

by:Doug Walton
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Sat80
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:Sat80
Comment Utility
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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
Yes, its working now, thanks :)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now