count an array


#1

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


#2

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


#3

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


#4

substr_count() maybe?

$var = “hello hello hello”;

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

echo $string;


#5

thankyou very much.


#6

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


#7

how is the output 3?


#8

‘is’ is contained 3 times in the string.


#9

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