tags:

views:

140

answers:

6

I need to get the last character of a string. Say I have "testers" as input string and I want the result to be "s". how can I do that in PHP?

+5  A: 
substr($string, -1) 
knittl
There's no PHP function called "substring", you'll get an error unless you've made your own substring function. Should be "substr".
Rich Adams
ahh you spoil the whole joke. +8 voting for the wrong answer looks just amazing :)
Col. Shrapnel
duh…, i meant `substr` of course
knittl
+1  A: 

Substr

Russell Dias
+1  A: 

use substr

and strlen

Hendrik
+14  A: 
substr("testers", -1); // returns "s"
Rich Adams
If you’re using multibyte character encodings like UTF-8, use `mb_substr` (http://php.net/mb_substr) instead.
Gumbo
+2  A: 

there are many ways to do it in PHP
Most common one is to ask google for the substring php and land on the page full of examples: http://php.net/manual/en/function.substr.php including your case too.

Col. Shrapnel
+1  A: 

Or by direct string access:

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