Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Doing if statements based on part of a string

    I am trying to do a bunch of if and elseif based on part of a variable.

    All the variables I want to process in the same manner contain the same string:

    submit_xxx
    submit_yyy
    submit_zzz etc..

    So I would like to do something like this:

    if ($_GET['var'] OPERATOR "submit_") {....

    Does such an operator exists? Or do I have to do use a "==" for each variable I want to process? I guess what I am looking for is an operator that means "contain".

    Thanks for any help.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    There are many functions for string manipulation.
    See www.php.net/substr
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Mar 2008
    Location
    Lakeland, FL
    Posts
    39
    Thanks
    1
    Thanked 3 Times in 3 Posts
    PHP Code:

    if(preg_match('/submit\_/i'$var))
    {
    //do stuff

    That will catch all strings with submit_ in them. Since I'm not sure how your getting $var I didn't include any code. Have no idea if your looping through and array or testing variables hard coded.

  • #4
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    I don't know if it makes any difference to use digitalfiz's example or mine, but this is what I use to look for a value within a variable:

    PHP Code:
    if (strpos($_GET['var'], 'submit_') === FALSE) {
        
    //$_GET['var'] does NOT contain submit_
    } else {
        
    //$_GET['var'] DOES contain submit_

    http://www.php.net/strpos

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    strpos() is much faster than preg_match().


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •