glompt Posted January 18, 2011 Share Posted January 18, 2011 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 Quote Link to comment Share on other sites More sharing options...
Garethp Posted January 19, 2011 Share Posted January 19, 2011 That will only work if that number is the only thing in the entire file, with no leading or trailing spaces and no line break at the end. Put a else on that, to output $file to see what the actual contents are, if you can't match it, that way you can fine out why Quote Link to comment Share on other sites More sharing options...
salathe Posted January 19, 2011 Share Posted January 19, 2011 Use the m pattern modifier with your existing regex. P.S. Life would be easier if you also used the file_get/put_contents functions. http://php.net/reference.pcre.pattern.modifiers http://php.net/file_get_contents http://php.net/file_put_contents 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.