Argh, why won't this work?

Discussion in 'PHP' started by bobby9101, Apr 4, 2007.

  1. #1
    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
     
    bobby9101, Apr 4, 2007 IP
  2. tippie

    tippie Peon

    Messages:
    660
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    what does it need to do and what's the error?

    did you chmod elist.txt?
     
    tippie, Apr 4, 2007 IP
  3. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    bobby9101, Apr 4, 2007 IP
  4. klown

    klown Peon

    Messages:
    2,093
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    $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:
     
    klown, Apr 4, 2007 IP
  5. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It returns nothing if it can't find the name, but it returns the starting point if it finds it.
    So it is working
     
    bobby9101, Apr 4, 2007 IP
  6. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok I got it fixed, I used gettype, to figure out that it returns an integer as the data type, then used !is_int()
     
    bobby9101, Apr 4, 2007 IP
  7. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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:
     
    sea otter, Apr 4, 2007 IP