grabbing the last word in a sentence

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
abgoosht
Forum Newbie
Posts: 5
Joined: Sun Oct 26, 2008 8:28 pm

grabbing the last word in a sentence

Post by abgoosht »

I have a string with n number of words and I'm trying to take the last word. Do you have any suggestions, I can't seem to get it....

ex.
$someString = 'my name is john';
$lastWord = some_function ($someString);

some_function ($variable) {
?
?
?
}
sirish
Forum Newbie
Posts: 8
Joined: Tue Sep 09, 2008 11:45 pm

Re: grabbing the last word in a sentence

Post by sirish »

Each word in the sentence is separated by a space so the following function will work

function some_function($variable){
$variable="my name is john";
$tmpArr=explode(" ",$variable);
$last_index=count($tmpArr)-1;
return $tmpArr[$last_index];
}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: grabbing the last word in a sentence

Post by s.dot »

Code: Select all

function lastWord($sentence)
{
    return array_pop(explode(' ', $sentence));
}
Test:

Code: Select all

$sentence = 'gimme da last word of this here string';
echo lastWord($sentence);
Result:

Code: Select all

string
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: grabbing the last word in a sentence

Post by Jenk »

Code: Select all

strrchr($string, ' ');
may want to trim $string.
abgoosht
Forum Newbie
Posts: 5
Joined: Sun Oct 26, 2008 8:28 pm

Re: grabbing the last word in a sentence

Post by abgoosht »

thanks a lot guys. i actually ended up using this

Code: Select all

 
        $str = 'here is some string and we want the last word';
        $str = trim($str);
        $temp=explode(" ", $str);
        echo $temp[count($temp)-1];
 
André D
Forum Commoner
Posts: 55
Joined: Thu Aug 28, 2008 7:03 pm

Re: grabbing the last word in a sentence

Post by André D »

That works fine, but I would prefer strrchr(), like Jenk suggested. It requires less code and is more efficient.

Code: Select all

$str = 'here is some string and we want the last word';
echo trim(strrchr($str, ' '));
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: grabbing the last word in a sentence

Post by onion2k »

André D wrote:That works fine, but I would prefer strrchr(), like Jenk suggested. It requires less code and is more efficient.

Code: Select all

$str = 'here is some string and we want the last word';
echo trim(strrchr($str, ' '));
That fails if the string is only 1 word long, eg there's no spaces.

All the solutions posted so far fail if there's punctuation in the string ... eg "What is the last word?" will return "word?".
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: grabbing the last word in a sentence

Post by pickle »

This looks like it should work:

Code: Select all

$target = 'Can I have the last word please?';
$pattern = "/.* ([\w-']*).*/";
preg_match($pattern,$target,$matches);
echo $matches[1];
I could only think of a hyphen and apostrophe (though it's a single-quote there) as special characters that show up in words. Obviously the pattern can be updated.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply