Welcome to WebmasterWorld Guest from 54.160.131.144

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Last two words

     

Mr_Cat

9:57 am on Jan 27, 2012 (gmt 0)

5+ Year Member



Hi folks,

I'm trying to get the last two words from a database entry.
I found this code which gets the last word, but am wondering how to modify it for different numbers of words. I tried adding a few things to the trim statement but to no avail.

function lastWord($sentence) {

$words = explode(' ', $sentence);
$result = trim($words[count($words) - 1 ], '.?![](){}*');

return $result;
}

$string = $mystring;

Any ideas how to modify this or perhaps a different method most welcome!

penders

10:59 am on Jan 27, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Based on your script...

/** 
* Returns the last N words from the passed string
* @param string $text The original text to analyse
* @param int $numWords (OPTIONAL) The number of words to return, default 1
* @return string
*/
function getLastWordsStr($text,$numWords=1) {
$nonWordChars = ':;,.?![](){}*';
$result = '';
$words = explode(' ',$text);
$wordCount = count($words);
if ($numWords > $wordCount) {
$numWords = $wordCount;
}
for ($w = $numWords; $w > 0; $w--) {
if (!empty($result)) {$result .= ' ';}
$result .= trim($words[$wordCount - $w], $nonWordChars);
}
return $result;
}

eelixduppy

12:43 pm on Jan 27, 2012 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



Nicely done, penders. :)

Only improvement I can see being made here is that this function is assuming that all words are going to be separated by a single space character. Might perhaps be safer to replace the explode with a preg_split to account for varying whitespace, eg:


$words = preg_split('\W+', $text, 0, PREG_SPLIT_NO_EMPTY);


I believe this would also make the trim unnecessary.

Mr_Cat

11:00 am on Jan 28, 2012 (gmt 0)

5+ Year Member



Totally brilliant, works a treat as I expect you knew it would :D Thanks penders you clever person

penders

6:23 pm on Jan 28, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



You're welcome :)

$words = preg_split('\W+', $text, 0, PREG_SPLIT_NO_EMPTY);


That's an idea. Although /\W+/ could potentially break up words that contain punctuation, like "don't", etc. Should you split hyphenated words? You could use /\s+/ to handle other kinds of white-space, but you'd still need to trim. Maybe split on non-word chars except the apostrophe:
'/[^\'\w]+/'
and then avoid the trim?

Another improvement to make it more general... instead of having 1 function. Have a helper function that simply returns an array of all the words. Then it would be easy to write getFirstWordsStr(), getLastWordsStr(), getMidWordsStr() that simply array_slice()'s this array of words and implode() to build a string (if reqd).

Mr_Cat

8:13 pm on Jan 28, 2012 (gmt 0)

5+ Year Member



Even better, and even more suited to my ultimate requirements :D
It also sounds like a neat little project for me to learn a bit more with! I'll try and bash something together.

Cheers