Connecting Tech Pros Worldwide Forums | Help | Site Map

Show only last word in a parameter

houghi
Guest
 
Posts: n/a
#1: Nov 16 '08
I have a parameter $name that contavins e.g. "Turn left at Blooming
Grove Turnpike/Quassaic Turnpike/RT-94 Continue to follow RT-94".
I just want to have the last word. The number of words will be different
each time.

I looked at http://be.php.net/manual/en/book.strings.php but I either
did not see it or I am looking at the wrong place (or both)

houghi
--
Quote:
> Beware of he who would deny you access to information, <
> for in his heart he dreams himself your master. <
> Commissioner Pravin Lal: "U.N. Declaration of Rights" <

houghi
Guest
 
Posts: n/a
#2: Nov 16 '08

re: Show only last word in a parameter


houghi wrote:
Quote:
I have a parameter $name that contavins e.g. "Turn left at Blooming
Grove Turnpike/Quassaic Turnpike/RT-94 Continue to follow RT-94".
I just want to have the last word. The number of words will be different
each time.
>
I looked at http://be.php.net/manual/en/book.strings.php but I either
did not see it or I am looking at the wrong place (or both)
As often happens, I found it right after posting, evin if I was looking
for it for a long time:

$name="your text with words. try to find the last word using several
spaces before the last word gsvdfg5412f_tw4t=agast3)(@35.\n";
preg_match("/(?<=\040)([^\s]+?)$/",trim($name),$matches);
$name = $matches[0];

Found it on http://www.programmingtalk.com/showthread.php?t=12649

houghi
--
Quote:
> Beware of he who would deny you access to information, <
> for in his heart he dreams himself your master. <
> Commissioner Pravin Lal: "U.N. Declaration of Rights" <
Jerry Stuckle
Guest
 
Posts: n/a
#3: Nov 16 '08

re: Show only last word in a parameter


houghi wrote:
Quote:
I have a parameter $name that contavins e.g. "Turn left at Blooming
Grove Turnpike/Quassaic Turnpike/RT-94 Continue to follow RT-94".
I just want to have the last word. The number of words will be different
each time.
>
I looked at http://be.php.net/manual/en/book.strings.php but I either
did not see it or I am looking at the wrong place (or both)
>
houghi
Well, you could use something like:

$name = trim($name); // Strip leading/trailing white space
$pos = strrpos($name, ' '); Find the last space
if ($pos === false) // If there is none
$word = $name; // Then only one word
else
$word = substr($name, $pos+1); // Get everything following the space
echo $word;

Assuming, of course, you always have a space as a word separator.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
houghi
Guest
 
Posts: n/a
#4: Nov 16 '08

re: Show only last word in a parameter


Jerry Stuckle wrote:
Quote:
Well, you could use something like:
>
$name = trim($name); // Strip leading/trailing white space
$pos = strrpos($name, ' '); Find the last space
if ($pos === false) // If there is none
$word = $name; // Then only one word
else
$word = substr($name, $pos+1); // Get everything following the space
echo $word;
>
Assuming, of course, you always have a space as a word separator.
It is, thanks for the feedback.

houghi
--
Quote:
> Beware of he who would deny you access to information, <
> for in his heart he dreams himself your master. <
> Commissioner Pravin Lal: "U.N. Declaration of Rights" <
Closed Thread