strpos multiple patterns

Discussion in 'PHP' started by joshm, Nov 10, 2007.

  1. #1
    Hi. I hope someone can help me here. I'm sure it's an easy solution but I cant figure it out right now.

    Basically the code below returns False if the $user id does not start with '2' and have 5 characters. So, 20000 is true for example.

    if (strpos($user, '2') !== 0 && strlen($user) == 5) {
    		return false;
    	} else {
    		return true;
    	}
    }
    PHP:
    Now, how can I make it return False if $user id does not start with either '1' or '2', or '3' and is 5 characters long?
     
    joshm, Nov 10, 2007 IP
  2. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    if ($user{0} == 2 && strlen($user) == 5) {
        return true;
    } else {
        return false;
    }
    PHP:
     
    decepti0n, Nov 10, 2007 IP
  3. joshm

    joshm Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sorry I don't really follow. I want it to return false if $user does not start with either 1, 2, or 3 and is 5 chars long. So, 1XXXX, 2XXXX, 3XXXX would return true for example, otherwise it should be false.
     
    joshm, Nov 10, 2007 IP
  4. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I suppose if you want anything between 10,000 and 39,999 (or with letters i suppose, but just for clarity) to return true, try:
    if (in_array($user{0}, array(1,2,3)) && strlen($user) == 5) {
        return true;
    } else {
        return false;
    }
    PHP:
     
    decepti0n, Nov 11, 2007 IP
  5. joshm

    joshm Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks. It's working well.
     
    joshm, Nov 11, 2007 IP
  6. meannn

    meannn Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks........
     
    meannn, Jan 5, 2010 IP