note 55818 deleted from function.str-word-count by danbrown

From: Date: Fri, 31 Aug 2012 16:28:39 +0000
Subject: note 55818 deleted from function.str-word-count by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190376@lists.php.net to get a copy of this message
Note Submitter: jtey at uoguelph dot ca ---- 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)); ?>

« previous php.notes (#190376) next »