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, [EMAIL PROTECTED] ([EMAIL PROTECTED]) 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, [EMAIL PROTECTED] ([EMAIL PROTECTED]) 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
Re: Re[2]: [PHP] counting words in a string
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 [EMAIL PROTECTED] To: Richard Kurth [EMAIL PROTECTED]; php-general [EMAIL PROTECTED] 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, [EMAIL PROTECTED] ([EMAIL PROTECTED]) 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, [EMAIL PROTECTED] ([EMAIL PROTECTED]) 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re[2]: [PHP] counting words in a string
wouldn't it be easier and more efficient to simply count the number of spaces in the string (and add 1)? using substr_count or something similar olinux --- Justin French [EMAIL PROTECTED] wrote: 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, [EMAIL PROTECTED] ([EMAIL PROTECTED]) 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, [EMAIL PROTECTED] ([EMAIL PROTECTED]) 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 __ Do you Yahoo!? New DSL Internet Access from SBC Yahoo! http://sbc.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php