Re: Extract from string
| From: | Steve Edberg | Date: | Sat, 14 Apr 2001 22:09:15 +0000 |
| Subject: | Re: Extract from string | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-48595@lists.php.net to get a copy of this message | ||
At 11:07 PM +0200 4/14/01, n e t b r a i n wrote:
Hi all, anyone have or know where I can find a small function in order to extract from a string the most relevant words in it? something like this: $var="I love coffe ... Coffe is from Brazil and coffe with milk .."; $occurence=2; //$occurence means word that are repeat 2 or more times my_dream_funct($var,$occurence); //the funct now return the word _ coffe _ many thanks in advance max ps.plz note: I need that it works on php3Well, just offthetopofmyhead: function word_occurrence($word,$phrase) {
$word = strtolower($word); # this way,$phrase = strtolower($phrase); # case is irrelevant $Bits = split($word.'[^[:alnum:]]*', $phrase); return (count($Bits)-1); } I tested this, and it works fine (php 3.0.12) EXCEPT it counts 'coffecoffe' as TWO words, not zero. If that's the behavior you want, then it's fine. Now I'm intrigued...I want to find a single regular expression that will NOT match 'coffecoffe'. Perhaps preg_ functions (available on PHP >= 3.0.9). And, I tried things like split('[^[:alnum:]]*'.$word.'[^[:alnum:]]*', " $phrase ") ...didn't work. -steve -- +----------- 12 April 2001: Forty years of manned spaceflight -----------+ | Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ | +-------------------------- www.yurisnight.net --------------------------+