Send to a Friend

wenn's avatar

PHP echo number of instances if specific word?

Asked by wenn (2673points) November 1st, 2009

I have exploded an array and found the word “and” in it, and i want to echo the number of times it occurs.

anyone enlighten me to a simple solution?

——————

$content = “array that is exploded, its pretty long so i just used this for example text but should return 4 ‘and’s ”;

$and = “and”;
$words = explode(” ”, $content);

if (in_array($and, $words)) {
echo ”‘and’ occurs this many times”;
}
else {
echo “none”;
}

Using Fluther

or

Using Email

Separate multiple emails with commas.
We’ll only use these emails for this message.