I need to check for MULTI occurances

Discussion in 'Programming' started by jc@ukzone.com, Mar 21, 2007.

  1. #1
    I need to check for multi occurances in a string.
    At the moment I am checking for a single occurance using strpos()
    e.g.
    $pos = strpos($notice, "abc");
    if ($pos !== false) {
    Dissallow notice
    }

    I now want to look for "abc" and "def" and "ghi"
    and then dissallow notice.

    Can anybody help me with the code for this.
    Or do I need to use the same code again for each occurance to look for.

    Thanks
     
    jc@ukzone.com, Mar 21, 2007 IP
  2. druidelder

    druidelder Peon

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could use an else if clause, or you could do three variables with a an 'or' clause, or you could repeat the code three times.

    1-----------------
    if(strpos($notice, "abc") !== false){
    Dissallow notice
    } elseif(strpos($notice, "def") !== false){
    Dissallow notice
    } elseif(strpos($notice, "ghi") !== false){
    Dissallow notice
    }else {
    Allow notice
    }

    2----------------------
    $pos1 = strpos($notice, "abc");
    $pos2 = strpos($notice, "def");
    $pos3 = strpos($notice, "ghi");
    if ($pos1 !== false) or ($pos2 !== false) or ($pos3 !== false) {
    Dissallow notice
    }

    3----------------
    $pos = strpos($notice, "abc");
    if ($pos !== false) {
    Dissallow notice
    }
    $pos = strpos($notice, "def");
    if ($pos !== false) {
    Dissallow notice
    }
    $pos = strpos($notice, "ghi");
    if ($pos !== false) {
    Dissallow notice
    }
     
    druidelder, Mar 21, 2007 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    preg_match_all returns the number of occurences found in a string, I'd make a simple function using that instead of all that messing around ....
     
    krakjoe, Mar 21, 2007 IP
  4. druidelder

    druidelder Peon

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #4
    jc's looking for the occurence of multiple patterns, not multiple occurences of the same pattern.
     
    druidelder, Mar 21, 2007 IP
  5. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #5
    which is why I suggested using regex and not static text, think it over .....
     
    krakjoe, Mar 21, 2007 IP
  6. jc@ukzone.com

    jc@ukzone.com Guest

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Many thanks to the people who replied.
    I used DRUIDELDER's suggestion. The only problem that I found was that I had to slightly modify this line:
    if ($pos1 !== false) or ($pos2 !== false) or ($pos3 !== false)
    to:
    if (($pos1 !== false) or ($pos2 !== false) or ($pos3 !== false))
    :D
    Thanks also to krakjoe for his suggestion.
    I couldn't use his suggestion because I am a novice and I didn't understand regex or preg_match_all
    :eek:

    Being a novice I needed the example that DRUIDELDER gave me, but I am very appreciative for all suggestions.

    Does anybody know how to do the same thing in PERL ???
    :confused:
     
    jc@ukzone.com, Mar 22, 2007 IP
  7. druidelder

    druidelder Peon

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You're welcome.
     
    druidelder, Mar 22, 2007 IP