get the last 7 characters of a PHP string

get the last 7 characters of a PHP string

Use substr() with a negative number for the 2nd argument.
$newstring = substr($dynamicstring, -7);

string substr ( string $string , int $start [, int $length ] )

If start is negative, the returned string will start at the start’th character from the end of string

Example:

<?php
echo substr(“Hello world”,0,10).”<br>”;   // output will be  Hello worl
echo substr(“Hello world”,1,8).”<br>”;  // output will be ello wor
echo substr(“Hello world”,0,5).”<br>”;
echo substr(“Hello world”,6,6).”<br>”;

echo substr(“Hello world”,0,-1).”<br>”;
echo substr(“Hello world”,-10,-2).”<br>”;
echo substr(“Hello world”,0,-6).”<br>”;
?>

 

 

 

One thought on “get the last 7 characters of a PHP string

Comments are closed.