...

View Full Version : PLEASE HELP! PHP get line from file and compare it not working.



ShadowIce
01-11-2010, 04:52 PM
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

/*** 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~

kbluhm
01-11-2010, 05:17 PM
$file = 'my_file.txt';
$myvar = 'b';

// Get all lines of the file as an array, ignoring new line characters and empty lines
$lines = file( $file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );

// does the value of $myvar exist in the array?
if ( FALSE !== array_search( $myvar, $lines ) )
{
// at least one line in $file contains the value in $myvar
}

ShadowIce
01-11-2010, 05:34 PM
Here's what I have so far:


<?php

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

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

// Get all lines of the file as an array, ignoring new line characters and empty lines
$lines = file( $file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );

if( file_exists( $file ) && is_readable( $file ) )
{
// does the value of $myvar exist in the array?
if ( FALSE !== array_search( $myvar, $lines ) )
{
echo $myvar;
}else{
echo "variable incorrect";
}
}
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;
}
?>

kbluhm
01-11-2010, 05:41 PM
My example above replaces the need for that rather inefficient readLine() function... I'm not sure why you're attempting to combine the two... and you're now checking for the existence and readability of the file after you get the contents with file().

You're also telling us what you have so far and nothing else. Is the script complete? Are there any errors? Are you looking for any additional advice or input?

Try this if you'd like to grab the line number that matched:


$file = 'my_file.txt';
$myvar = 'b';

// Get all lines of the file as an array, ignoring new line characters
$lines = file( $file, FILE_IGNORE_NEW_LINES );

// does the value of $myvar exist in the array?
if ( FALSE !== ( $num = array_search( $myvar, $lines ) ) )
{
// line number $num matched the value of $myvar
}


You could also user array_keys() in the place of array_search() if more than one line will potentially contain the value of $myvar and you'd like to return them all. array_search() will only return the first match.

ShadowIce
01-11-2010, 05:46 PM
Well, I incorperated your code into my if statement after checking to see if the file exists, and it is readable. I for some reason can't get it to work..

kbluhm
01-11-2010, 05:50 PM
Well, I incorperated your code into my if statement after checking to see if the file exists, and it is readable. I for some reason can't get it to work..

Are you understanding the use of file()?


Is the script complete? Are there any errors? Are you looking for any additional advice or input?

I have to run to the gym and get pumped. I'll be back in an hour or so to check on your progress.

ShadowIce
01-11-2010, 06:00 PM
Yes, I understand how to use file(). What I DON'T understand is how to get that if statement working correctly and such

ninnypants
01-11-2010, 06:08 PM
The if statement will return true if the coupon code is in the file because array_search will return the position of the code in the array so it would not be identical to false

ShadowIce
01-11-2010, 06:21 PM
so should i use readfile instead of readlines() or what?

ShadowIce
01-11-2010, 06:31 PM
How can I turn $myvar into an array of valid coupon codes?

Here's what I have so far:


<?php

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

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


// Get all lines of the file as an array, ignoring new line characters
$lines = file( $file, FILE_IGNORE_NEW_LINES );

// does the value of $myvar exist in the array?
if ( FALSE !== ( $num = array_search( $myvar, $lines ) ) )
{
// line number $num matched the value of $myvar

echo $lines;

}else{

echo "Lines that were read failed...";

}

?>

kbluhm
01-11-2010, 09:10 PM
Yes, I understand how to use file(). What I DON'T understand is how to get that if statement working correctly and such

I'm not so sure that I agree with you. In your latest code you are calling file( $file ) before you check whether $file exists and is readable. Even in your original code, you were calling it with readFile(), which accesses the file, then you check if the file exists and is readable.

Now if the file is always there, you would never even have know this flawed logic existed. Plus, if you always expect the file to be valid and readable, then there's no need for the file_exists() and is_readable() checks.




Back to your original question, which didn't make a lot of sense. You have a file with a bunch of lines in it. These lines are coupon codes.

You then have a variable that contains a coupon code as it's value. Your ultimate goal is to check the file to see if the coupon ode exists in the file.

Is this correct?

If so, will the coupon code exist more than once in the file? If so, you want to return 1) all line numbers that match, 2) the first line number that matches, or 3) just return TRUE or FALSE if it does or does not exist?

Don't give us any code. Just explain in great detail what you want it to do and I'm sure we can help you out.

ShadowIce
01-11-2010, 09:24 PM
um, dude? yea, i removed that if command...


<?php

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

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


// Get all lines of the file as an array, ignoring new line characters
$lines = file( $file, FILE_IGNORE_NEW_LINES );

// does the value of $myvar exist in the array?
if ( FALSE !== ( $num = array_search( $myvar, $lines ) ) )
{
// line number $num matched the value of $myvar

echo $lines;

}else{

echo "Lines that were read failed...";

}

?>

kbluhm
01-11-2010, 09:29 PM
Why are you echoing $lines? It is an array. You'll always see Array when you echo an array.

Anyone else want to help this guy?

ShadowIce
01-11-2010, 10:21 PM
ExCUSE me?! You know what fool! Just because I don't "automatically know the syntax in my head", doesn't mean you can't help me line-by-line so I know how to use it. I'm human too ya know, I'm willing to learn if someone is willing to teach me! So SCUUUUUUUUUUUUUUUUUUUUUUUSE me if I don't get it.... :@

JohnDubya
01-12-2010, 12:13 AM
That's about enough of this worthless banter back and forth.

kbluhm, if you can't or don't want to help ShadowIce any further, that's up to you. You're more than welcome to leave these topics for someone else. Thank you for your help so far.

ShadowIce, you have got to be polite in your responses. You are asking for people to make the comments that they are making. To you and everyone else: do a simple Google search about your topic before asking for an example of something here. SI, everything you have asked so far has already been covered somewhere that is searchable by Google. When you ask for simple examples that you can find yourself, it simply wastes everyone else's time.

So guys, please respect each other, do your own leg work before asking others to do it, and don't make me write another post like this. :)

ShadowIce
01-12-2010, 02:42 PM
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

ShadowIce
01-12-2010, 08:03 PM
How can I change $myCoupon into an array that checks for the specific array string already in the text file?


<?php
$coupons = file('my_file.txt');
$coupons = array_map('trim', $coupons); // Remove any unwanted white space from all elements
$myCoupon = array('w4t8uw4tj3w8ru8w3ur8q3ujr83w', 'w4t8uw4tj3w8ru8w3ur8q3ujr83w');
$comma_separated = implode(",", $myCoupon);

echo $comma_seperated;

if(in_array($myCoupon, $coupons) == true) {
$validCoupon = true;
}
else {
$validCoupon = false;
}

if($validCoupon == true) {
echo $myCoupon . ' is valid';
}
else {
echo 'Invalid coupon code: "' . $myCoupon . '"';
}

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum