Help comparing variable with lines from a file..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Help comparing variable with lines from a file..

Postby ShadowIce » Tue Jan 12, 2010 9:45 am

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.

Syntax: [ Download ] [ Hide ]
<?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:

Syntax: [ Download ] [ Hide ]
a
b
c
d
e
f
g
ShadowIce
Forum Commoner
 
Posts: 75
Joined: Tue Jan 12, 2010 9:43 am

Re: Help comparing variable with lines from a file..

Postby AlanG » Tue Jan 12, 2010 11:49 am

Read in the file as an array and then check if the coupon is a value in that array.

Syntax: [ Download ] [ Hide ]
<?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;
}
?>
"We all know Linux is great…it does infinite loops in 5 seconds." - Linus Torvalds
AlanG
Forum Contributor
 
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Help comparing variable with lines from a file..

Postby ShadowIce » Tue Jan 12, 2010 12:21 pm

It didn't work

Syntax: [ Download ] [ Hide ]
<?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."\"";
 
}
 
?>
ShadowIce
Forum Commoner
 
Posts: 75
Joined: Tue Jan 12, 2010 9:43 am

Re: Help comparing variable with lines from a file..

Postby AlanG » Tue Jan 12, 2010 1:37 pm

Syntax: [ Download ] [ Hide ]
<?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 . '"';
}
?>
"We all know Linux is great…it does infinite loops in 5 seconds." - Linus Torvalds
AlanG
Forum Contributor
 
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Help comparing variable with lines from a file..

Postby AbraCadaver » Tue Jan 12, 2010 1:47 pm

Probably wanted array_map():

Syntax: [ Download ] [ Hide ]
$coupons = array_map('trim', $coupons);
For PHP | Language reference | Function reference | 99% of what you need to know.

-Shawn
http://www.spidean.com

"There are only 10 types of people in the world: Those who understand binary, and those who don't."
User avatar
AbraCadaver
DevNet Resident
 
Posts: 2027
Joined: Mon Feb 24, 2003 11:12 am
Location: The Republic of Texas

Re: Help comparing variable with lines from a file..

Postby AlanG » Tue Jan 12, 2010 2:00 pm

AbraCadaver wrote:Probably wanted array_map():

Syntax: [ Download ] [ Hide ]
$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.

Syntax: [ Download ] [ Hide ]
<?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;
}
?>
"We all know Linux is great…it does infinite loops in 5 seconds." - Linus Torvalds
AlanG
Forum Contributor
 
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Help comparing variable with lines from a file..

Postby ShadowIce » Tue Jan 12, 2010 3:08 pm

How can I change $myCoupon into an array that checks for the specific array string already in the text file?

Syntax: [ Download ] [ Hide ]
<?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 . '"';
}
 
?>
ShadowIce
Forum Commoner
 
Posts: 75
Joined: Tue Jan 12, 2010 9:43 am

Re: Help comparing variable with lines from a file..

Postby AbraCadaver » Tue Jan 12, 2010 3:26 pm

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':

Syntax: [ Download ] [ Hide ]
$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 . '"';
}
For PHP | Language reference | Function reference | 99% of what you need to know.

-Shawn
http://www.spidean.com

"There are only 10 types of people in the world: Those who understand binary, and those who don't."
User avatar
AbraCadaver
DevNet Resident
 
Posts: 2027
Joined: Mon Feb 24, 2003 11:12 am
Location: The Republic of Texas

Re: Help comparing variable with lines from a file..

Postby ShadowIce » Tue Jan 12, 2010 3:27 pm

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
ShadowIce
Forum Commoner
 
Posts: 75
Joined: Tue Jan 12, 2010 9:43 am

Re: Help comparing variable with lines from a file..

Postby AbraCadaver » Tue Jan 12, 2010 3:38 pm

Syntax: [ Download ] [ Hide ]
$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 . '"';
    }
}
Last edited by AbraCadaver on Tue Jan 12, 2010 3:51 pm, edited 1 time in total.
For PHP | Language reference | Function reference | 99% of what you need to know.

-Shawn
http://www.spidean.com

"There are only 10 types of people in the world: Those who understand binary, and those who don't."
User avatar
AbraCadaver
DevNet Resident
 
Posts: 2027
Joined: Mon Feb 24, 2003 11:12 am
Location: The Republic of Texas

Re: Help comparing variable with lines from a file..

Postby AbraCadaver » Tue Jan 12, 2010 3:45 pm

Or actually if you want it to fail completely if any one fails, then:

Syntax: [ Download ] [ Hide ]
$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;
}
For PHP | Language reference | Function reference | 99% of what you need to know.

-Shawn
http://www.spidean.com

"There are only 10 types of people in the world: Those who understand binary, and those who don't."
User avatar
AbraCadaver
DevNet Resident
 
Posts: 2027
Joined: Mon Feb 24, 2003 11:12 am
Location: The Republic of Texas

Re: Help comparing variable with lines from a file..

Postby ShadowIce » Tue Jan 12, 2010 3:51 pm

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?
ShadowIce
Forum Commoner
 
Posts: 75
Joined: Tue Jan 12, 2010 9:43 am

Re: Help comparing variable with lines from a file..

Postby AbraCadaver » Tue Jan 12, 2010 4:13 pm

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.
For PHP | Language reference | Function reference | 99% of what you need to know.

-Shawn
http://www.spidean.com

"There are only 10 types of people in the world: Those who understand binary, and those who don't."
User avatar
AbraCadaver
DevNet Resident
 
Posts: 2027
Joined: Mon Feb 24, 2003 11:12 am
Location: The Republic of Texas

Re: Help comparing variable with lines from a file..

Postby ShadowIce » Tue Jan 12, 2010 4:16 pm

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! :P

Thanks ALL of you! I couldn't have done it w/o you! PROBLEM SOLVED! :)
ShadowIce
Forum Commoner
 
Posts: 75
Joined: Tue Jan 12, 2010 9:43 am

Re: Help comparing variable with lines from a file..

Postby ShadowIce » Tue Jan 12, 2010 7:31 pm

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?

Syntax: [ Download ] [ Hide ]
<?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);
        }
 
}
 
?>
ShadowIce
Forum Commoner
 
Posts: 75
Joined: Tue Jan 12, 2010 9:43 am

Next

Return to PHP - Code

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest