php reading files and moving down lines

Associate
Joined
19 Jul 2006
Posts
1,847
oh i can get php to read from a text file.
what i want to do now is put the first line into a variable then the second line in to a second variable. check something then make the first variable the same as the 3rd line
Psedo code
Variable 1 = first line
Variable 2 = second line
"do something"
clear variable 1 and 2
Variable 1 = third line
varible 2 = forth line
ect repeat till end of file.
 
PHP:
$val = file_get_contents('./file.txt');
$val2 = explode("\n", $val);
$line1 = $val2[0];
$line2 = $val2[1];
echo "Do something";
$line3 = $val2[2];

Hope this helps :-)

if you don't know how many lines there will be, use

PHP:
$len = count($val2);
 
that works a treat, is there a way of instead of putting
$line3
$line4 ect for each one of the lines.
as i dont know how many lines there will be
getting it to automatically create the smae amount of line variables as there is lines in the text.
 
Code:
$lines = file('file.txt');
for ( $i = 0; $i < count($lines); $i += 2 ) {
    if ( $some_condition && isset($lines[$i - 2]) && isset($lines[$i - 1]) ) {
        $lines[$i - 2] = $lines[$i];
        $lines[$i - 1] = $lines[$i + 1];
    }
}
$fh = fopen('file.txt', 'w');
fwrite($fh, join("\n", $lines));
fclose($fh);

Edit: wait, you don't want to swap the lines, you just want to read them and check each one?

Code:
$lines = file('file.txt');
for ( $i = 0; $i < count($lines); $i += 2 ) {
    // line one/three/five/etc: $lines[$i]
    // line two/four/six/etc: $lines[$i + 1]
}
 
cheers rob that works for the
$lines[$i]
but if i try to
PHP:
echo "$lines[$i + 1]";
i get
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting ']' in C:\xampplite\htdocs\passwordsite\read.php on line 14
i tried getting rid of the spaces or putting in [$i+[1]] and other variations but still get same result
 
hargi said:
cheers rob that works for the
$lines[$i]
but if i try to
PHP:
echo "$lines[$i + 1]";
i get
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting ']' in C:\xampplite\htdocs\passwordsite\read.php on line 14
i tried getting rid of the spaces or putting in [$i+[1]] and other variations but still get same result

Code:
echo $lines[$i + 1];
 
http://php.net/echo

PHP:
echo 'This is a string, I cannot interpolate variables, e.g. $hi'; // Does not output $hi;
echo "This is a string which allows interpolation: $hi"; // Outputs $hi
echo "Double quotes can also interpolate array values, e.g. {$arr['hi']}"; // Displays array element with key 'hi'
echo $hi, __SENTENCE__; // No quotes for variables, constants etc.
echo $arr['hi']; // Echo an array value

// My preference is single quotes, using echo's function-esque syntax:
echo 'Hi, my name is ', $name, '.';

// But you can also concatenate:
echo 'Hi, my name is ' . $name . '.';

Single quotes = strings only
Double quotes = strings with variable/array interpolation
No quotes = variables, constants etc.
 
Back
Top Bottom