count an array

heres an easy one, i just wanna know how to count how many … words… are in an array.

wait no… in my case it isn’t an array, nm. nm

heres a better question: how can i count the amount of times a certain string shows up in a variable. so example:

$var = “hello hello hello”;

return 3

whats that fuction called

substr_count() maybe?

$var = “hello hello hello”;

$string = substr_count($var, “hello”);

echo $string;

thankyou very much.

Carella is right that will work, just be careful when using it though cause say you have a string like the following:

[php]

<? $str = "This is the best dish!"; $count = substr_count($str, "is"); echo $count; ?>

[/php]

The output will be: 3

how is the output 3?

‘is’ is contained 3 times in the string.

whoa, it is! I didn’t look close enough.

Sponsor our Newsletter | Privacy Policy | Terms of Service