How to Get the Last Character in a String with PHP

November 7th, 2010 - Posted by Steve Marks to Web Development.

There a number of ways to get the last character in a string by using PHP. The simplest way I find however is to use the substr() function like so:

$str = "This is a string";
echo substr($str, -1);

The above example would return ‘g’. By passing a negative value as the second parameter we can tell PHP to start at the nth character from the end.

This entry was posted on Sunday, November 7th, 2010 at 3:07 pm by +Steve Marks and is filed under Web Development. You can follow any responses to this entry through the RSS 2.0 feed.

Fear not, we won't publish this

Comments (0)

No comments have been left yet. Be the first