Witold Kowelski
Witold Kowelski

Reputation: 922

Smart substr_count?

Okay, so suppose I have a string as follows: $string = "0::15:6:0";

Now, I want to count the number of numbers above 0 in the string, so I substr_count($string, ':') + 1) which provides me with the number 5 (which is technically correct, since although there are 4 actual numbers, the blank is used as a 0 in my code). But, that includes the 0's and blank 0's...which I do not want.

Any suggestions on how to make a "smart substr_count" to render me the number 2 (since there are two numbers not blank or 0, being 15 and 6)?

Upvotes: 0

Views: 442

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324630

Awesomeness in a tiny package:

$count = count(array_filter(explode(":",$string)));

This works because the string "0" is falsy.

Upvotes: 4

Related Questions