glompt Posted December 31, 2010 Share Posted December 31, 2010 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 Quote Link to comment Share on other sites More sharing options...
johnny86 Posted December 31, 2010 Share Posted December 31, 2010 Because you have line break after every number you can match it with preg_match: preg_match('/^' . $number . '$/', $file); ^ matches begining of a string and $ matches the end of the string. Quote Link to comment Share on other sites More sharing options...
glompt Posted December 31, 2010 Author Share Posted December 31, 2010 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. 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.