Connecting Tech Pros Worldwide Help | Site Map

Show only last word in a parameter

  #1  
Old November 16th, 2008, 12:45 PM
houghi
Guest
 
Posts: n/a
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" <
  #2  
Old November 16th, 2008, 01:15 PM
houghi
Guest
 
Posts: n/a

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" <
  #3  
Old November 16th, 2008, 01:35 PM
Jerry Stuckle
Guest
 
Posts: n/a

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
==================
  #4  
Old November 16th, 2008, 02:35 PM
houghi
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mail merge to Word giladp1@gmail.com answers 7 June 12th, 2007 09:25 AM
Regular Expressions in C# LordHog@hotmail.com answers 3 May 23rd, 2006 07:55 AM
Error Trapping In Access 2000 Peter Frost answers 6 November 12th, 2005 11:14 PM
Error Trapping In Access 2000 Peter Frost answers 6 November 12th, 2005 10:50 PM