(PHP help) 4 equal words

-3

So I'm doing a project with the word 'Dog' that looks in the test file .. if you have 4 'Dog' it's the result, if not ==> (Dog) Does not exist or has less than 3 words in the test .txt but I'm not able to put '3' greater than 3

project structure

<?php
    $arquivo = strtolower(file_get_contents('teste.txt'));
    $textoBuscar = strtolower('Cachorro');

    if(strpos($arquivo, $textoBuscar )!== FALSE){
        echo '<h1>mais de 3 palavras ((Cachorro)) no teste.txt< /h1>';
    } else {
        echo '<h1>(Cachorro) Não Existe ou tem menos de 1 palavra  no teste.txt< /h1>';

    }

?>
    
asked by anonymous 24.10.2018 / 22:56

1 answer

2
  

The function preg_match_all () will return an integer with the number of occurrences found by the regular expression.

<?php

$arquivo = file_get_contents('cachorro.txt');
$count = preg_match_all("/Cachorro/", $arquivo, $matches);

if($count>3)
{

   echo '<h1>mais de 3 palavras ((Cachorro)) no teste.txt Tem '.$count.'< /h1>';

} else {

   echo '<h1>(Cachorro) Não Existe ou tem '.$count.' no teste.txt< /h1>';

}
?>

Example in ideone

  

The "i" after the delimiter indicates a case-insensitive search

preg_match_all("/Cachorro/i", $arquivo, $matches);
    
24.10.2018 / 23:46