In a string like "abc fox fox fox ghi xyz" how can i get the number of times 'fox' is repeated in the string?
From stackoverflow
-
$string = 'abc fox fox fox ghi xyz'; $substring = 'fox'; $substringCount = substr_count($string, $substring); echo '"' . $substring . '" appears in "' . $string . '" ' . $substringCount . ' times';Paolo Bergantino : Aaah. beat me to it. :)alex : That's a first Paolo!! :)Lucas Jones : There's a builtin for that? Wow! Named differently from every other string function, I presume! ;)alex : PHP consistency can be a bit tricky sometimes... but php.net is only a few clicks away 99% of the time :)Phil Carter : found this function today, was hard to find, not being next to the str* or str_* functionsjsims281 : Ha! substringCount! That's awesome, a very elegant way to do it.
0 comments:
Post a Comment