...

View Full Version : get the last character of a string?



Dubz
11-22-2011, 02:51 AM
I'm wondering how you can grab the last character of a string.
For example:


$string = "The quick brown fox jumps over the lazy dog";

echo $string{0};
//Echoes the letter T

echo $string{5};
//Echoes the letter u


My question is, how can you get the LAST character of the string without knowing its length? I tried using -1 but ad no luck (unless it was in another part that it came out wrong). If their isn't another way I could always use strlength and get it but I'm just wondering if theirs an actual way for this

Adee
11-22-2011, 02:55 AM
echo substr($string, -1, 1);

or


echo $string[strlen($string)-1];

jassi.singh
11-22-2011, 10:27 AM
Check this manual

http://php.net/manual/en/function.substr.php

This will help you in future with functions



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum