4

I want to check whether a string is present in another string, and then take the appropriate action. What is the best way to do this?

For instance; if the string 'europe' is present in 'europeisthebest', then do something.

if ( isIn('europe', 'europeisthebest') == true){
  //do something
}

Thank you a lot! I appreciate all of your answers and time spent helping.

2 Answers 2

12

You're looking for strstr() (case sensitive), stristr() (case insenstive), or strpos()

if (stristr('europeisthebest', 'europe')){
  // do something
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hello John. Thank you. This is what I was looking for. I tried strpos previously but for some reason It wouldnt act correctly when I added ===true after the condition. Everything's good now. Thank you alot my friend.
@bornie That's because it doesn't return a boolean true; rather, it returns an integer if the string is found or a boolean false if it isn't. Using strpos to search for a string within a string can be accomplished like this: if( strpos('blah', 'blah') !== false ).
my string was like EX3.19 or EX7.99 and I wanted to search EX in the string... maybe because this might have caused the confusion... I dont know. Thank you for your answer.
0

You can also use the function strpos() if you wish to know the position of the substring inside the string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.