Find occurrences of a word

1

I have a text and I would like to find certain words, but in the way I am doing it it is with 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 give 0 because as the function is case sensitive will not find me the other words, how to circumvent it?

    
asked by anonymous 28.06.2015 / 17:46

1 answer

1

To avoid case sensitive you just have to turn both strings into uppercase or lowercase, eg:

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

That way your phrase will turn:

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

And your other argument turns:

"MENINO"
  

2 occurrences

IdeOne Example

    
28.06.2015 / 17:55