Jump to content

PLEASE HELP! PHP get line from file and compare it not working.


ShadowIce

Recommended Posts

Hi. Can someone please make it so that this code that I made reads through every line in a file, and checks it against the $myvar variable to see if its equal to what its reading in the file?

 

PHP Code:

 

<?php

/*** make sure the file exists ***/
$file = 'my_file.txt';

$myvar = "b";
$couponcode = readLine($file, 2);

//2nd line of the text file should be b

if( file_exists( $file ) && is_readable( $file ) )
{
if($myvar != $couponcode){ //if b!=b
    echo $myvar."!=".$couponcode;
}else{
    echo $myvar."!";
}
}
else
{
    echo "Cannot read from $file";
}
?>

<?php

/**
*
* Read a line number from a file
*
* @param    string    $file    The path to the file
* @param    int    $line_num    The line number to read
* @param    string    $delimiter    The character that delimits lines
* @return    string    The line that is read
*
*/
function readLine($file, $line_num, $delimiter="\n")
{
    /*** set the counter to one ***/
    $i = 1;

    /*** open the file for reading ***/
    $fp = fopen( $file, 'r' );

    /*** loop over the file pointer ***/
    while ( !feof ( $fp) )
    {
        /*** read the line into a buffer ***/
        $buffer = stream_get_line( $fp, 1024, $delimiter );
        /*** if we are at the right line number ***/
        if( $i == $line_num )
        {
            /*** return the line that is currently in the buffer ***/
            return $buffer;
        }
        /*** increment the line counter ***/
        $i++;
        /*** clear the buffer ***/
        $buffer = '';
    }
    return false;
}
?>

 

ANY HELP is GREATLY appreciated!

 

Shadow~

you are not reading every line in the file. only one. you need to use a while loop on the readLine result to read all the lines in the file.

also, your flow could be failing on the logic if (file_exists($file) && is_readable($file)). But you should be getting an the error message if that's the case.

while ($couponcode = readLine($file, 2))
{
   ...
}
check that the value of the line you are retrieving is correct by printing it
[code=php:0]
print($couponcode);

No... but if you do, you should be able to fix your own code instead of trying to get someone else to do it. The answer is you're reading one line, maybe try reading all of them... I'll point you in the right direction file

and if i wanted to make $myvar an array, and make the variable that reads the file into an array and compare them, how would i do that? and NO smartass answers please, if u DO have a smartass answer, please just completely ignore this thread. I don't hijack YOUR threads..

I hear the best way to get help is to be a jerk and ignore the people that already answered the question. You need to be in a freelance forum since you don't want to even try to code this yourself. You're being silly and frankly are making an @ss of yourself :)

 

Hope that helps

well.. from what i read of your code, doing the while() loop with readLine in it should work AFAIK just like i displayed in my first reply... but failing that, if you want to read an entire file to an array just use the pre-included function file(): http://au2.php.net/manual/en/function.file.php

 

And examples of use:

// Get a file into an array.  In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://www.example.com/');

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}

// Another example, let's get a web page into a string.  See also file_get_contents().
$html = implode('', file('http://www.example.com/'));

// Using the optional flags parameter since PHP 5
$trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

ok, now how can I use $myvar to compare it to every string in my_file.txt and return whether or not it matches the text in the file?

 

<?php
// Get a file into an array.  In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('my_file.txt');

$myvar = "b";

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
if($myvar = htmlspecialchars($line)."<br \>\n"){
//    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
    //echo $myvar." "."matches ".htmlspecialchars($line)."<br \>\n";
      echo trim($myvar." "."matches ".htmlspecialchars($line)."<br />\n");
}else{
    //echo $myvar." doesn't match ".htmlspecialchars($line)."<br />\n";
}
}

// Another example, let's get a web page into a string.  See also file_get_contents().
$html = implode('', file('my_file.txt'));

// Using the optional flags parameter since PHP 5
$trimmed = file('my_file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
?>

 

my_file.txt:

 

a
b
c
d
e
f
g

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.