Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Get the last character of a string?
Old 12-15-2007, 05:03 PM Get the last character of a string?
feraira's Avatar
BeTheBand!

Posts: 350
Trades: 0
How is it possible to retrieve the last character of a string?

I want to be able to find out the last character of a string, and if it is a certain character do something (obviously i know this will be through an if statement).

I figured out how to break the string into an array, but then using that array how do i get the last character?

Thanks
feraira is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-15-2007, 05:11 PM Re: Get the last character of a string?
Extreme Talker

Posts: 238
Location: United States
Trades: 0
PHP Code:
$char substr($string, -1); // returns the last character of  $string 

Reference
__________________
The interlocking pieces of web development: usability, performance, accessibility, and standards.
frost is offline
Reply With Quote
View Public Profile
 
Old 12-16-2007, 09:46 PM Re: Get the last character of a string?
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
If you use frost's example above, you won't need to break the string into an array. But to answer another question you have, you can access the last element of an array by using:

echo end($array);
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 12-19-2007, 02:12 AM Re: Get the last character of a string?
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
$char = $string[strlen($string)-1]; // you can refer to a string as to array without breaking
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 12-19-2007, 11:41 AM Re: Get the last character of a string?
feraira's Avatar
BeTheBand!

Posts: 350
Trades: 0
Thanks all
feraira is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Get the last character of a string?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.21574 seconds with 12 queries