note 60492 deleted from function.str-word-count by nlopess

From: Date: Mon, 09 Jan 2006 12:38:39 +0000
Subject: note 60492 deleted from function.str-word-count by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-101446@lists.php.net to get a copy of this message
Note Submitter: Peter Mescalchin ---- This might be too simplistic. I have had a play with this function - which is good, but I feel this regular expression seems to fit my needs better. <?php $text = 'Lo-rem ipsum dolor-1956 sit amet, consectetuer adipiscing elit. Curabitur elementum. Pellentesque porta ipsum ac neque. Vestibulum id ipsum. Cras suscipit ornare arcu. Donec egestas. Mauris lorem felis, aliquet vitae, dictum a, pellentesque vitae, arcu. Sed viverra justo. Ut auctor, purus at iaculis congue, orci nisi interdum enim, sit amet posuere sapien nunc eget nunc. Proin metus dolor, interdum et, dictum vehicula, placerat id, est. Curabitur vitae augue. '; $exp = '/([a-zA-Z][a-zA-Z-]+[a-zA-Z])[^a-zA-Z-]/'; preg_match_all($exp,$text,$matches); print_r($matches[1]); ?> Basically all words must start and end with a character A-Z plus words may be joined using a hyphen (dash). The not condition of [^a-zA-Z-] seems to work well as it means a word will end on space, question mark, line feed, full stop, etc, etc. The only limits to this regexp I can see are: 1) Your input string must end with a non A-Z character (space/fullstop/etc.) otherwise the last word in your string will not be matched and 2) strings/words such as 'dolor-1956' will never match (is that an issue anyway?). Can anyone see any glaring floors in my logic?

« previous php.notes (#101446) next »