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.";
}



14 Comments      

14 Comments to PHP Find Variable in String Comments Feed

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.”);

- collideous

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

- Anurag Tarar

Thanks !
This is exactly what i am looking for :)
Vinit

- Vinit

Thank you, thank you, thank you!

- Jason

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!

- me

damn cant belive i always forget this one!

- douche

douche - That’s exactly why I posted it. :)

- Thomas

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

- ontengen

$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.”;
}

- ontengen

THANKS! I really needed this! YOU ROCK!

- sumsumin

Thank you, I searched all over for this

- Greg

You, are, Legend.

Exactly the last piece to my puzzle!

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

- Paul

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.

- darklord

nice. :) Thanx.

- Ben Althauser

Leave Your Thoughts

Some comments may be held for moderation and not appear immediately. Those with links and/or harsh language may need my approval before showing up live. I may also remove or edit any that I feel are spammy in nature or just plain rude.

Subscribe without commenting


Plurk.com

Welcome www.google.com visitor!

More results for php search string.

TwisterMc Themes For Firefox 3 Connect with me via a social network. Web Host
Tumbler Badge
Archives
In Expensive Quality Stock Photos Made on a Mac