Re: counting words in a string

From: Date: Sat, 28 Sep 2002 06:22:42 +0000
Subject: Re: counting words in a string
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-117921@lists.php.net to get a copy of this message
if this is your SPECIFIC problem, putting a space at the beginning of $srch will help, eliminating XAVAILABLE... but this will cause a problem with the word AVAILABLE appearing at the start of the string, so temporarily put a space at the start of the string: <? // UNTESTED $count = substr_count(' '.strtolower($string), strtolower(' '.$srch)); echo $count; ?> this won't help if there are newlines and other white space instead of spaces, and won't help (so far) for a different set of circumstances... it would be nice to extend substr_count() could be extended to have an option. Justin on 28/09/02 3:41 PM, rdkurth@starband.net (rdkurth@starband.net) wrote: > > I need to count how many times the word AVAILABLE appears in a string > like this > $string ="AVAILABLE More Info AVAILABLE More Info"; > some time the string looks like this > $string ="UNAVAILABLE More Info AVAILABLE More Info"; > or > $string ="AVAILABLE More Info UNAVAILABLE More Info"; > when I use > $srch="AVAILABLE"; > $count=substr_count(strtolower($string), strtolower($srch)); > echo $count; > it puts the count a 2 even when one of the words is UNAVAILABLE > how can I make it only count AVAILABLE and not UNAVAILABLE or visa > verse

« previous php.general (#117921) next »