1

first i'm sorry for bad title, my english is not good enough and my little problem is not plain (for me).

Anyway, i have a rudimental script that counts how many times appears a list of words in a string.

$orto1 = substr_count($text," fingger ");
$orto2 = substr_count($text," apears ");
$orto3 = substr_count($text,"ghiven");

$ortog = $orto1 + $orto2 + $orto3;

Now, it's very uncomfortable whenever i've to add a new word, so, i'd like put all words in a array.

So, i've tried with substr_count as well as several examples found here, unsuccessfully.

I'd like to manage to create something like that:

$array = array(" fingger ", " apears ", "ghiven", "suporting");
$total = substr_count($array, $mytext);
echo $total;

Important is case insensitive and "wildcard" matching.

Is it possible or there is another best way?

Thank you at all! Riccardo

2 Answers 2

4

I guess a simple loop will do?

$total = 0
$array = array(" fingger ", " apears ", "ghiven", "suporting");
foreach($array as $word){
  $total += substr_count($word, $mytext);
}
echo $total;

Regarding the case-insensitivity you might use

substr_count(strtoupper($word), strtoupper($mytext))
Sign up to request clarification or add additional context in comments.

Comments

0

OMG! I had not thought at the simple way like a loop :-)

Anyway, there is a little error:

  $total += substr_count($word, $mytext);

The correct way is:

   $total += substr_count($mytext, $word);

Thank you so much!!!

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.