473,402 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Show only last word in a parameter

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
--
> 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" <
Nov 16 '08 #1
3 2770
houghi wrote:
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
--
> 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" <
Nov 16 '08 #2
houghi wrote:
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.
js*******@attglobal.net
==================
Nov 16 '08 #3
Jerry Stuckle wrote:
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
--
> 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" <
Nov 16 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bimo Remus | last post by:
Hi, I am currently taking a C++ class and am having problems with a homework assignment. My problem is that I need to pull the first and last words out of of a character string array which is in...
2
by: Larry C | last post by:
Hello, I have an XML doc and I am trying to write a utility thath will allow me to change a particular word with my "title" section of theXML doc. I would like to look for a "word" in the title...
3
by: Lynn A. | last post by:
Access/Word 2000 I am trying to merge a word document with an access query that has a parameter attached to it. Could someone please suggest how to stop the parameter from appearing when the...
0
by: Gary Miller | last post by:
I read the MS article on Word Object and coded as implied. I instantiate a Word app: Word.Application app = new Word.Application(); Then Open a HTML document: (I’ve tried all Opens, OLD,...
2
by: Mikey | last post by:
Sample VB .NET source code to create mailing labels or customized letters using MS Word MailMerge This VB .NET source code will start MS Word and call methods and set properties in MS Word to...
4
by: Charlie Brookhart | last post by:
I am trying to write a code for a button click event. When the button is clicked, it is supposed to bring up an open file dialog box to allow the user to select the document they which to open....
1
by: Bob | last post by:
I have the follwing snippet of code, lets me find a document and then open it, but thw word application does not show up. I had similara to this in vb6 and it worked, but I can't get it to show...
4
by: kdubble | last post by:
Hi I am trying to get the results of a query to show only unique student records (not duplicates). Is there a simple way to make the criteria field do this? I am not too familiar with SQL. ...
1
by: simonyong | last post by:
hello, everyone Sorry for disturb.. im newbie to asp.net im trying to do a task that as below: In a SAME datagrid, it ll show all data when user key in key word in textbox... datagrid will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.