homepage Welcome to WebmasterWorld Guest from 54.161.175.231
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Last two words
Mr_Cat




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

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




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

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




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

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




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

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

penders




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

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




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved