note 60492 added to function.str-word-count
| From: | PeterMescalchin at osu1 dot php dot net | Date: | Mon, 09 Jan 2006 02:45:07 +0000 |
| Subject: | note 60492 added to function.str-word-count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-101429@lists.php.net to get a copy of this message | ||
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?
----
Manual Page -- http://www.php.net/manual/en/function.str-word-count.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60492
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60492&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60492&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60492&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60492&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60492&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60492&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60492&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60492&report=yes
Search -- http://master.php.net/manage/user-notes.php