Is there a function that searches if a word exists in the text?

Asked

Viewed 64 times

3

In PHP there is some função checking words in text, example:

A camila morreu de diabete.

I want to create a function looking for the word morreu, for after they find playing in a if the result, getting more or less like this:

if (função == 'morreu') { echo 'OK'; } else { echo 'FAIL'; }

Is there any function?

  • Verification is a very generic term, you need to say what you want to do. You want to know if the word is contained in the text?

  • Yes, I wonder if the word is in texto, and then be able to create a IF with her in you.

1 answer

4


Has the strpos(). Or stripos() if you want to ignore the letters box. In some cases you may want to search for the end, perhaps for performance, with strrpos().

These functions return false if the searched text is not in the full text, or returns the position of the first occurrence of the text if it is found. So you can test this, something like that:

if (strpos('A camila morreu de diabete', 'morreu') !== false) { echo 'OK'; } else { echo 'FAIL'; }

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.