ShadowIce Posted January 11, 2010 Share Posted January 11, 2010 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~ Quote Link to comment Share on other sites More sharing options...
Catfish Posted January 11, 2010 Share Posted January 11, 2010 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); Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted January 11, 2010 Author Share Posted January 11, 2010 and how exactly would i read EVERY line, and compare the $myvar variable with EVERY line? Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted January 11, 2010 Share Posted January 11, 2010 you need to use a while loop on the readLine result to read all the lines in the file. wow Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted January 11, 2010 Author Share Posted January 11, 2010 um, excuse me, do u even know what readLine() does? oO It reads A... A! line of the file.. while ($couponcode = readLine($file, 2)) { Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted January 11, 2010 Share Posted January 11, 2010 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 Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted January 11, 2010 Author Share Posted January 11, 2010 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.. Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted January 11, 2010 Share Posted January 11, 2010 No... but you do break the forum rules Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted January 11, 2010 Author Share Posted January 11, 2010 Does it look like I care? there ARE other forums out there ya know. Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted January 11, 2010 Share Posted January 11, 2010 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 Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted January 11, 2010 Share Posted January 11, 2010 ANY HELP is GREATLY appreciated! pffft Quote Link to comment Share on other sites More sharing options...
Catfish Posted January 12, 2010 Share Posted January 12, 2010 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); Quote Link to comment Share on other sites More sharing options...
ShadowIce Posted January 12, 2010 Author Share Posted January 12, 2010 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 Quote Link to comment Share on other sites More sharing options...
Catfish Posted January 12, 2010 Share Posted January 12, 2010 if($myvar == htmlspecialchars($line)."<br \>\n") change = to == Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.