Page 1 of 3
Help comparing variable with lines from a file..
Posted: Tue Jan 12, 2010 8:45 am
by ShadowIce
Hi all. 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? I'm doing this so I can create a coupon code for my customers.
Code: Select all
<?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:
Re: Help comparing variable with lines from a file..
Posted: Tue Jan 12, 2010 10:49 am
by AlanG
Read in the file as an array and then check if the coupon is a value in that array.
Code: Select all
<?php
$coupons = file('coupons.txt');
array_walk($coupons, 'trim'); // Remove any unwanted whitespace from all elements
$myCoupon = 'a';
if(in_array($myCoupon, $coupons) == true) {
$validCoupon = true;
}
else {
$validCoupon = false;
}
?>
Re: Help comparing variable with lines from a file..
Posted: Tue Jan 12, 2010 11:21 am
by ShadowIce
It didn't work
Code: Select all
<?php
$coupons = file('my_file.txt');
array_walk($coupons, 'trim'); // Remove any unwanted whitespace from all elements
$myCoupon = 'a';
if(in_array($myCoupon, $coupons) == true) {
$validCoupon = true;
}
else {
$validCoupon = false;
}
if($validCoupon){
echo $coupons." is valid";
}else{
echo "Invalid coupon code: \"".$coupons."\"";
}
?>
Re: Help comparing variable with lines from a file..
Posted: Tue Jan 12, 2010 12:37 pm
by AlanG
Code: Select all
<?php
function trimValue(&$value) {
$value = trim($value);
}
$coupons = file('my_file.txt');
array_walk($coupons, 'trimValue'); // Remove any unwanted whitespace from all elements
$myCoupon = 'b';
if(in_array($myCoupon, $coupons) == true) {
$validCoupon = true;
}
else {
$validCoupon = false;
}
if($validCoupon == true) {
echo $myCoupon . ' is valid';
}
else {
echo 'Invalid coupon code: "' . $myCoupon . '"';
}
?>
Re: Help comparing variable with lines from a file..
Posted: Tue Jan 12, 2010 12:47 pm
by AbraCadaver
Probably wanted array_map():
Code: Select all
$coupons = array_map('trim', $coupons);
Re: Help comparing variable with lines from a file..
Posted: Tue Jan 12, 2010 1:00 pm
by AlanG
AbraCadaver wrote:Probably wanted array_map():
Code: Select all
$coupons = array_map('trim', $coupons);
Yup that's the one. Was thinking 'trim' worked before... :-S No need for the custom function then.
Updated solution.
Code: Select all
<?php
$coupons = file('my_file.txt');
$coupons = array_map('trim', $coupons); // Remove any unwanted white space from all elements
$myCoupon = 'b';
if(in_array($myCoupon, $coupons) == true) {
$validCoupon = true;
}
else {
$validCoupon = false;
}
?>
Re: Help comparing variable with lines from a file..
Posted: Tue Jan 12, 2010 2:08 pm
by ShadowIce
How can I change $myCoupon into an array that checks for the specific array string already in the text file?
Code: Select all
<?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 . '"';
}
?>
Re: Help comparing variable with lines from a file..
Posted: Tue Jan 12, 2010 2:26 pm
by AbraCadaver
I have no idea what you're trying to do now. What's with the array and the implode?
If 'w4t8uw4tj3w8ru8w3ur8q3ujr83w' exists on its own line in 'my_file.txt', then the following code will display 'w4t8uw4tj3w8ru8w3ur8q3ujr83w is valid':
Code: Select all
$coupons = file('my_file.txt');
$coupons = array_map('trim', $coupons); // Remove any unwanted white space from all elements
// Why are you doing this???
// $myCoupon = array('w4t8uw4tj3w8ru8w3ur8q3ujr83w', 'w4t8uw4tj3w8ru8w3ur8q3ujr83w');
// $comma_separated = implode(",", $myCoupon);
// echo $comma_seperated;
// Just do this!
$myCoupon = 'w4t8uw4tj3w8ru8w3ur8q3ujr83w';
if(in_array($myCoupon, $coupons) == true) {
$validCoupon = true;
}
else {
$validCoupon = false;
}
if($validCoupon == true) {
echo $myCoupon . ' is valid';
}
else {
echo 'Invalid coupon code: "' . $myCoupon . '"';
}
Re: Help comparing variable with lines from a file..
Posted: Tue Jan 12, 2010 2:27 pm
by ShadowIce
I'm trying to compare multiple strings w/ the multiple strings in the file already.
So if I have:
a
b
in the file, then I would like to check for both of them all at the same time.
i also want to make it so that I can only use that certain code ONCE
Re: Help comparing variable with lines from a file..
Posted: Tue Jan 12, 2010 2:38 pm
by AbraCadaver
Code: Select all
$coupons = file('my_file.txt');
$coupons = array_map('trim', $coupons); // Remove any unwanted white space from all elements
$myCoupon = array('a', 'b');
$myCoupon = array_unique($myCoupon); // Remove duplicates from array
foreach($myCoupon as $value) {
if(in_array($value, $coupons)) {
echo $value . ' is valid';
}
else {
echo 'Invalid coupon code: "' . $value . '"';
}
}
Re: Help comparing variable with lines from a file..
Posted: Tue Jan 12, 2010 2:45 pm
by AbraCadaver
Or actually if you want it to fail completely if any one fails, then:
Code: Select all
$coupons = file('my_file.txt');
$coupons = array_map('trim', $coupons); // Remove any unwanted white space from all elements
$myCoupon = array('a', 'b');
$myCoupon = array_unique($myCoupon); // Remove duplicates from array
foreach($myCoupon as $value) {
if(in_array($value, $coupons)) {
$validCoupon = true;
}
else {
$validCoupon = false;
break;
}
if($validCoupon == true) {
echo implode(',', $myCoupon) . ' are valid';
}
else {
echo 'Invalid coupon code: ' . $value;
}
Re: Help comparing variable with lines from a file..
Posted: Tue Jan 12, 2010 2:51 pm
by ShadowIce
Two FINAL questions.
How can I make it so that when a user submits the code, $myCoupon checks the array of file strings and returns if it's a valid string. And how can I make it so the user can only use that code ONCE?
Re: Help comparing variable with lines from a file..
Posted: Tue Jan 12, 2010 3:13 pm
by AbraCadaver
ShadowIce wrote:Two FINAL questions.
How can I make it so that when a user submits the code, $myCoupon checks the array of file strings and returns if it's a valid string. And how can I make it so the user can only use that code ONCE?
You're quickly moving into database territory.
Your first question is already solved by the code that has been posted, as far as I can tell.
As for your second question, there is no way to guarantee this, however you can have the user logon and then store their user id and coupon in a db and check it when they submit another one. Depending upon how the user is signed up, they could always register again with a different username or email or whatever and use the coupon again.
Re: Help comparing variable with lines from a file..
Posted: Tue Jan 12, 2010 3:16 pm
by ShadowIce
Not if I have it read a file that tells it which ones NOT to use

I'll simply have it log which coupon they use, and then have it go into another file. if that coupon is in there, then disable then from using it. thanks man!

and yes i got ALL that from ur short, yet simple sentence man! Thanks!
Thanks ALL of you! I couldn't have done it w/o you! PROBLEM SOLVED!

Re: Help comparing variable with lines from a file..
Posted: Tue Jan 12, 2010 6:31 pm
by ShadowIce
ok, new problem. I've decided that i want to delete the coupon code in the file that the user has already used. that way, it is not used twice. how do i accomplish this?
Code: Select all
<?php
$coupons = file('my_file.txt');
$coupons = array_map('trim', $coupons); // Remove any unwanted white space from all elements
$myCoupon = array('a', 'b');
foreach($myCoupon as $value) {
if(in_array($value, $coupons)) {
echo $value . ' is valid'.'<br>';
}
else {
echo 'Invalid coupon code: "' . $value . '"<br>';
}
}
// the line to delete
$lineNum = 87;
delLineFromFile('my_file.txt', $lineNum);
function delLineFromFile($fileName, $lineNum){
// check the file exists
if(!is_writable($fileName))
{
// print an error
print "The file $fileName is not writable";
// exit the function
exit;
}
else
{
// read the file into an array
$arr = file($fileName);
}
// the line to delete is the line number minus 1, because arrays begin at zero
$lineToDelete = $lineNum-1;
// check if the line to delete is greater than the length of the file
if($lineToDelete > sizeof($arr))
{
// print an error
print "You have chosen a line number, <b>[$lineNum]</b>, higher than the length of the file.";
// exit the function
exit;
}
//remove the line
unset($arr["$lineToDelete"]);
// open the file for reading
if (!$fp = fopen($fileName, 'w+'))
{
// print an error
print "Cannot open file ($fileName)";
// exit the function
exit;
}
// if $fp is valid
if($fp)
{
// write the array to the file
foreach($arr as $line) { fwrite($fp,$line); }
// close the file
fclose($fp);
}
}
?>