PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old May 8th, 2008, 11:54 AM
martin9348 martin9348 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 65 martin9348 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 10 h 59 m 43 sec
Reputation Power: 4
PHP-General - Lenght last word in a string

Hello,

I am trying to find a way to calculate the "lenght" of the last word in a string.

E.g.

$string = "This is the last word";

lenght of last word: 4

$string = "This is the last word with a space ";

lenght of last word: 5

$string = "Hola Hi Bonjour w";

lenght of last word: 1

I really don't know how to do it even if it shouldn't be that complicated..

Thank you in advance for your help!

Reply With Quote
  #2  
Old May 8th, 2008, 12:17 PM
kicken's Avatar
kicken kicken is offline
Wiser? Not exactly.
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: May 2001
Location: Ft Myers, FL
Posts: 4,003 kicken User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)kicken User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)kicken User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)kicken User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)kicken User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)kicken User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)kicken User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)kicken User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)kicken User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)kicken User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)kicken User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)  Folding Points: 115392 Folding Title: Super Ultimate Folder - Level 1Folding Points: 115392 Folding Title: Super Ultimate Folder - Level 1Folding Points: 115392 Folding Title: Super Ultimate Folder - Level 1Folding Points: 115392 Folding Title: Super Ultimate Folder - Level 1Folding Points: 115392 Folding Title: Super Ultimate Folder - Level 1Folding Points: 115392 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 2 Weeks 4 Days 16 h 44 m 28 sec
Reputation Power: 414
Send a message via ICQ to kicken Send a message via AIM to kicken Send a message via MSN to kicken
Quote:
Originally Posted by martin9348
Hello,

I am trying to find a way to calculate the "lenght" of the last word in a string.

E.g.

$string = "This is the last word";

lenght of last word: 4

$string = "This is the last word with a space ";

lenght of last word: 5

$string = "Hola Hi Bonjour w";

lenght of last word: 1

I really don't know how to do it even if it shouldn't be that complicated..

Thank you in advance for your help!


1) Use rtrim() to remove ending spaces (and other unwanted characters)
2) Use strrpos() to find the position of the last space.
3) Use substr() to remove the last word
4) Use strlen() to get the length.
5) It's "length" not "lenght"
__________________
Spidermonkey Tutorial

http://wiser.aoeex.com/ - Long term project (offline due to evil crawlers and lack of content)
http://www.aoeex.com/gmap.php - Put yourself on the map

Reply With Quote
  #3  
Old May 8th, 2008, 12:40 PM
martin9348 martin9348 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 65 martin9348 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 10 h 59 m 43 sec
Reputation Power: 4
Thanks, I'll work on it..

Quote:
Originally Posted by kicken
1) Use rtrim() to remove ending spaces (and other unwanted characters)
2) Use strrpos() to find the position of the last space.
3) Use substr() to remove the last word
4) Use strlen() to get the length.
5) It's "length" not "lenght"

Reply With Quote
  #4  
Old May 8th, 2008, 12:56 PM
thedude2010 thedude2010 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 247 thedude2010 User rank is Sergeant Major (2000 - 5000 Reputation Level)thedude2010 User rank is Sergeant Major (2000 - 5000 Reputation Level)thedude2010 User rank is Sergeant Major (2000 - 5000 Reputation Level)thedude2010 User rank is Sergeant Major (2000 - 5000 Reputation Level)thedude2010 User rank is Sergeant Major (2000 - 5000 Reputation Level)thedude2010 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 23 h 35 m 10 sec
Reputation Power: 33
you could do it this way also
PHP Code:
 $string="This is a String ";
$string_exploded=explode(" ",trim($string)); 
$length=str_len($string_exploded[count(string_exploded)-1]); 


Quote:
Originally Posted by martin9348
Thanks, I'll work on it..

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - Lenght last word in a string


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway