I have a list of first names in a text file And I trying so see if a name exists, if it doesn't then add the name. Here is what I have, why isn't it working? $regexp = '/^([a-z]|[A-Z])+$/'; $fc = file_get_contents("elist.txt"); $check = $_POST[name]; if (isset($_POST[name]) && preg_match($regexp, $_POST[name]) && !strpos($fc, $check)) { ... PHP: I took out the "&& !strpos($fc, $check)", and it works, except it will add the name even if it is already in the list (obviously) Thanks a bunch
Umm no error, it just adds the name even if it is already in the list. The writing isn't the problem, I want it to not write the name if the name already is in the file
$regexp = '/^([a-z]|[A-Z])+$/'; $fc = file_get_contents("elist.txt"); $check = $_POST[name]; if (isset($_POST[name]) && preg_match($regexp, $_POST[name]) && !strpos($fc, $check)) { PHP: strpos can do some strange things, try echoing your strpos to find out what the value of it is going to be when the name is on the list then change your if to be if (isset($_POST[name]) && preg_match($regexp, $_POST[name]) && strpos($fc, $check)==value) PHP:
It returns nothing if it can't find the name, but it returns the starting point if it finds it. So it is working
Ok I got it fixed, I used gettype, to figure out that it returns an integer as the data type, then used !is_int()
Your strpos check should look like this: strpos($fc, $check)===false PHP: Keep in mind that you're doing a case-sensitive check here, so if the name comes in via $_POST with a different (or mixed) case, it will get added to the list. Bob != BOB != bob when using strpos. If case doesn't matter (and I assume it doesn't given your regexp), use stripos instead. On that note, you could also simplify your regexp a little as such: $regexp = '/^([a-zA-Z])+$/'; PHP: