1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Check for string inside a string...

Discussion in 'PHP' started by envoy, Feb 21, 2006.

  1. #1
    Hey,

    I need some help here. I need to check whether a string is present inside another string, and if it is then do whatever...

    So for example, I have a string of:
    "ACTIVE: string 0 & string 1 DEACTIVE:"
    I need to check that string and see if "string 0 & string 1" is inside it, because it could be different all the time, could just be "string 0" one time, "string 1" another time, or "string 0 & string 1" some other time. This may sound confusing, but hopefully someone get's the idea. If not, i can try and provide more information. Thanks.
     
    envoy, Feb 21, 2006 IP
  2. tflight

    tflight Peon

    Messages:
    617
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Probably the easiest way to do it is with the strpos function. Example 1 at that URL is pretty much what you are looking to do.
     
    tflight, Feb 21, 2006 IP
  3. justinwp

    justinwp Peon

    Messages:
    81
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if (strpos($haystack,$needle)) { echo "Needle Found"; }

    strpos()
     
    justinwp, Feb 21, 2006 IP
  4. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $filestring="your text string";
    $findme  = 'some word';
    
    $pos = strpos($filestring, $findme);
    
    // Note the use of ===.  Simply == would not work as expected
    // because the position of 'a' was the 0th (first) character.
    if ($pos === false) {
       echo "The string '$findme' was not found in the url '$theurl'";
    } else {
       echo "The string '$findme' was found in the url '$theurl'";
       echo " and exists at position $pos";
    }
    PHP:
     
    mad4, Feb 22, 2006 IP