Nexy Posted July 8, 2008 Share Posted July 8, 2008 Why Hello There! How would I be able to something like this: if($text contains a specific word == "word1" && $text contains another specific word == "word2") { do something; } Basically, if word1 and word2 are in the variable $text in respective order, then do something. Also, $text can contain any other words in there too. So if there was something like this: $text = "Hello word1 there! Welcome to word2 today!"; Then the if statement would see those 2 words are in there in order. But if it's something like this: $text = "Hello word2 there! Welcome to word1 today!"; Then do nothing, since "word1" is not first. I hope that made sense. Any help would be appreciated. Thank You! Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 8, 2008 Share Posted July 8, 2008 try this if(!strstr(strstr($text,"word1"),"word2")===false){ strstr will return the part of the string after it finds the first occurrence so you search for word1 in Hello word1 there! Welcome to word2 today you get there! Welcome to word2 today so then you search again for word to and if it cant find it it will return FALSE Scott. Quote Link to comment Share on other sites More sharing options...
jonsjava Posted July 8, 2008 Share Posted July 8, 2008 my solution: <?php $text = "this is a test"; $word1 = "this"; $word2 = "test"; if ((strstr($text, $word1) && strstr($text, $word2)) && strpos($text, $word1) < strpos($text, $word2)){ print "it's true!"; } ?> Quote Link to comment Share on other sites More sharing options...
Nexy Posted July 8, 2008 Author Share Posted July 8, 2008 Thank You, both of you!! Both methods worked perfectly. :) :) Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 8, 2008 Share Posted July 8, 2008 but wouldn't that take longer because it does two strstr like mine and also does two strpos checks Scott. Quote Link to comment Share on other sites More sharing options...
jonsjava Posted July 8, 2008 Share Posted July 8, 2008 you are correct. I was just giving him another option, because I had already written it while you were posting. Hate to waste code. Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted July 8, 2008 Share Posted July 8, 2008 or you may also try: <?php if(strpos(strstr($mystring, $word1), $word2) !== false) { echo "Match found"; } ?> the reason i replaced the strstr() in the last one with strpos() is because we only aim to get the occurence other than the string itself since strpos() is faster and less memory intensive function. if you want to totally remove strstr(), you may use strpos() then just apply the third parameter offset which is the result of the first strpos() + strlen() of the first string... which i think may only become more costly, though I haven't used this one and untested. EDIT: oh, somebody made a post already... ahahah Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.