note 55818 added to function.str-word-count

From: Date: Sun, 14 Aug 2005 14:59:52 +0000
Subject: note 55818 added to function.str-word-count
Groups: php.notes 
Request: Send a blank email to php-notes+get-93640@lists.php.net to get a copy of this message
In reply to muz1's post below: You can also take advantage of using other built in PHP functions to get to your final result. Consider the following: <?php $string = "One two three four five six seven eight nine ten."; // the first n words to extract $n = 3; // extract the words $words = explode(" ", $string); // chop the words array down to the first n elements $firstN = array_slice($words, 0, $n); // glue the 3 elements back into a spaced sentence $firstNAsAString = implode(" ", $firstN); // display it echo $firstNAsAString; ?> Or to do it all in one line: <?php echo implode(" ", array_slice(explode(" ", $string), 0, $n)); ?> ---- Manual Page -- http://www.php.net/manual/en/function.str-word-count.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+55818 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55818&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55818&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55818&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55818&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55818&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55818&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+55818&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#93640) next »