Jump to content

glompt

New Members
  • Posts

    3
  • Joined

  • Last visited

    Never

Everything posted by glompt

  1. Hi. I'm trying to write a little php script that will take an input number from a text field and check it against a text file to see if it is already there, if it is it says so, if not, it adds it to the list. Where i'm having issues is with defining my regex; more specifically, I want it to handle 110 and 11 differently. Here is my current code so far that works: <html><body> <h4>DT</h4> <form action="test.php" method="post"> ID#: <input name="number" type="text" /> <input type="submit" /> </form> <?php $number = $_POST['number']; echo "$number"; //search $filename = "dtdatatxt"; $handle = fopen($filename, "r"); $file = fread($handle,filesize($filename)); if (preg_match('#' . $number . '#', $file, $match)) { echo " Sorry, Already visited today"; exit; } else { fclose($file); $file=fopen(dtdata.txt,"a") or exit("Unable to open file!"); fwrite($file, "$number"); fwrite($file, "\n"); fclose($file); echo " Accepted!"; exit; } ?> </body></html> If I'm not mistkaken, the correct preg_match statement should be if (preg_match('/^' . $number . '$/', $file, $match)) { However, when I use this statement, it always returns false. Any ideas? Thanks
  2. hi, thanks for the reply. I'm using the preg_match in an if statement statement and changed it to reflect what you suggested: if (preg_match('/^' . $number . '$/', $file, $match)) { However; using this causes the preg_match to always return false. Any idea why? Thanks again.
  3. Hi. I need some help getting my preg_match regex working with a variable input. I'm trying to limit visits to once per day, after you come the first time, you are added to data.txt and will be denied if you come again. The code works, but I need to limit the searches using ^ and $ and I'm having some difficulty. $number will be a number input from an html form ranging from 1-1000+ and I want to make sure that 1,10,100 etc., aren't treated as the same. here is my preg_match, how do I use regular expressions with $number? preg_match("/$number/", $file, $match) from what I have read, it should be like this, but it doesn't work preg_match('/^+$' , "/$number/", $file, $match) heres the full code $filename = "data.txt"; $handle = fopen($filename, "r"); $file = fread($handle,filesize($filename)); if (preg_match("/$number/", $file, $match)) { echo " Sorry, Already visited today"; exit; } else { fclose($file); $file=fopen(data.txt,"a") or exit("Unable to open file!"); fwrite($file, "$number"); fwrite($file, "\n"); fclose($file); echo " Accepted!"; exit; } Thanks
×
×
  • 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.