Jump to content

if statement preg_match help


glompt

Recommended Posts

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.