PHP Function To Get The Last N Characters From A String

Last modified: 
Tuesday, May 5th, 2015
Topics: 
PHP

This is a little PHP function to get the last n characters in a string. As you can see, it's pretty simple to do this with the native substr() function. I just forget how to do it a lot, so wrapping it in a method makes life a little easier for me.

<?php
  /**
  * Returns the last $n chararacters of a $str.
  *
  */
  public function LastNChars($str, $n)
  {
    return substr($str, (-1*$n), -1);
  }
?>


The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.