Find occurrences of a word

Asked

Viewed 58 times

1

I have a text and would like to find certain words, but in the way I’m doing that is with the substr_count() I can only find the word as I wrote it.

Example

$text = "Eu sou um Menino e gosto de brincar com outros MENINOS";
$count= substr_count($text , 'menino');
echo $count;

The result of this will be 0 because as the function is case sensitive won’t find me the other words, the way around this?

  • You can leave the search and string in low or high box.

1 answer

1


To avoid the case sensitive just turn both strings in capital letters or minuscules, for example:

$count= substr_count(strtoupper($text), strtoupper("menino"));

That way your sentence will turn:

"EU SOU UM MENINO E GOSTO DE BRINCAR COM OUTROS MENINOS"

And your other argument turns:

"MENINO"

2 occurrences

Ideone Exemplo

Browser other questions tagged

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