Re: Re[2]: [PHP] counting words in a string

From: Date: Sat, 28 Sep 2002 20:11:31 +0000
Subject: Re: Re[2]: [PHP] counting words in a string
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-117979@lists.php.net to get a copy of this message
You can get all the terms into an array using preg_match_all:- $str = "UNAVAILABLE More Info AVAILABLE More Info"; $regExp = "/(?:UN)?AVAILABLE|More Info/"; preg_match_all($regExp, $str, $regs); $regs[0] will contain the matched terms - you should then be able to use array functions to do whatever else you need to do - counting, positions, etc Debbie ----- Original Message ----- From: "Justin French" <justin@indent.com.au> To: "Richard Kurth" <rdkurth@starband.net>; "php-general" <php-general@lists.php.net> Sent: Saturday, September 28, 2002 6:26 PM Subject: Re: Re[2]: [PHP] counting words in a string > You need to look at a few options... one is regular expression (not my > forte), or perhaps winding through the string one character at a time, > writing a very simple state engine. > > Justin French > > > on 28/09/02 4:47 PM, rdkurth@starband.net (rdkurth@starband.net) wrote: > > > Hello Justin, > > > > That worked perfect but I have one more problem I need to know if one > > of the word is UNAVAILABLE I need to know if it is the first one or > > the second one. I don't know if there is any way to do this. > > 1st 2nd > > $string ="UNAVAILABLE AVAILABLE More Info"; > > 1st 2nd > > $string ="AVAILABLE More Info UNAVAILABLE "; > > > > Friday, September 27, 2002, 11:22:42 PM, you wrote: > > > > > > JF> if this is your SPECIFIC problem, putting a space at the beginning of > > $srch > > JF> will help, eliminating XAVAILABLE... but this will cause a problem with > > the > > JF> word AVAILABLE appearing at the start of the string, so temporarily put a > > JF> space at the start of the string: > > > > JF> <? > > JF> // UNTESTED > > JF> $count = substr_count(' '.strtolower($string), strtolower(' '.$srch)); > > JF> echo $count; > > ?>> > > > > JF> this won't help if there are newlines and other white space instead of > > JF> spaces, and won't help (so far) for a different set of circumstances... > > > > JF> it would be nice to extend substr_count() could be extended to have an > > JF> option. > > > > JF> Justin > > > > > > > > JF> 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 > > > > > > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.general (#117979) next »