Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Get nth character from end of string

Tags:

string

php

I'm sure there must be an easy way to get nth character from the end of string.

For example:

$NthChar = get_nth('Hello', 3); // will result in $NthChar='e'
like image 899
Subway Avatar asked Jan 24 '14 09:01

Subway


1 Answers

from substr examples

// Accessing single characters in a string
// can also be achieved using "square brackets"

thus:

$str = "isogram";
echo $str[-1]; // m
echo $str[-2]; // a
echo $str[-3]; // r
like image 96
widyakumara Avatar answered Sep 28 '22 06:09

widyakumara