Main Content

PHP Find Variable in String

Since I always try to remember what this code is, and I can never remember, I thought I’d add it to my blog so I never forget. 🙂

This searches a string of text and finds a user defined variable. Very handy.

$string = "PHP";
$container = "I love writing PHP code.";

if(strstr($container,$string)) {
echo "found it.";
} else {
echo "not found.";
}


26 Responses

  1. collideous says:

    Here’s another way to get the same result:

    $string = “PHP”;
    $container = “I love writing PHP code.”;

    echo (strstr($container,$string)?”found it.”:”not found.”);

  2. Anurag Tarar says:

    Hi,

    I have used the original method since it allows me to execute certain code based upon found or not found criteria. This article was very helpfull to me.

    Thanks

  3. Vinit says:

    Thanks !
    This is exactly what i am looking for 🙂
    Vinit

  4. Jason says:

    Thank you, thank you, thank you!

  5. me says:

    yeah, I don’t know why PHP net always has the complicated jibba jabba when dummies like myself use that site as well. I searched high and low for about ten minutes just now until you solved my problem in laymens terms. Thanks!

  6. douche says:

    damn cant belive i always forget this one!

  7. Thomas says:

    douche – That’s exactly why I posted it. 🙂

  8. ontengen says:

    … and for insensitive search like Jane, jaNE, or JANE

  9. ontengen says:

    $haystack=”Hi jane, you look great.”;
    $needle=”jaNe”;

    if (stristr($haystack,$needle)){
    print “Yup! i found it.”;
    }
    else{
    print “Nope, i don’t see it.”;
    }

  10. sumsumin says:

    THANKS! I really needed this! YOU ROCK!

  11. Greg says:

    Thank you, I searched all over for this

  12. Paul says:

    You, are, Legend.

    Exactly the last piece to my puzzle!

    Was struggling to write a “if .co.uk is in string then..” statement.

  13. darklord says:

    it’s cool but it can result only one.
    what can i do to get all find results.
    for example in
    “hello. i am a php developer. i am darklord”
    when i serach
    “i am”
    there are 2 in this paragraph.

  14. Ben Althauser says:

    nice. 🙂 Thanx.

  15. Pat says:

    Looks like I’m going to have to buy this guy a beer.

  16. Arnoud says:

    thanks, very helpfull!

    now i can continue making my own hitcounter!

  17. thanks thanksthanksthanksthanksthanksthanksthanks

  18. AJay says:

    Just wanted to say thanks for posting this. I always forget the easiest way to do this and have come across your site every time I look for the solution. You’ve helped me many times.

    -AJay

  19. shailesh says:

    thanks

  20. 7 says:

    Thank a lot.

  21. Daniel says:

    Thanks a lot!

  22. Thanks for this very helpful peice of code

    For some reason i always forget this one. they should call it search_str or something

  23. Frank says:

    ok, is there a way to search in string for several item previously collected in a array

    for example

    stristr($haystack,$array_needle)

  24. Polin says:

    How to implement with regular expreccion?, example error:

    $page = //This is te html content with url to $string
    $string = “/^http:\/\/bloat.me\/(.{4})/”;

    if(strstr($page,$string)) {
    echo “found it.”;
    } else {
    echo “not found.”;
    }

    tanks

  25. Johannes says:

    You should note that your code returns 0 (ie. the equivalent to false) if the needle is found in the very beginning of the haystack (position 0). To prevent this, use strict comparison:

    if(strstr($container,$string) === false) {
    echo “found it.”;
    } else {
    echo “not found.”;
    }

  26. Smail says:

    Thanks for sharing this piece of code, this helped me out a lot!

Leave a Reply