PHP – take last n characters of a string

Take last 3 characters when string is big enough

<?php
$str = "hello";
echo substr($str, -3);
?>
llo
Env: PHP 5.5.9 (Linux)

Take last 3 characters when string is smaller than 3 chars

<?php
$str = "he";
echo substr($str, -3);
?>
he
Env: PHP 5.5.9 (Linux)
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments