I understand that this question is from 2012, but the answers here are all ineffective. To do this, there are string functions built into PHP, instead of moving around the string and turning it into an array, and then selecting the last index, which is a lot of work to make something very simple.
The following code gets the last occurrence of a string in a string:
strrchr($string, '.');
We can use this in conjunction with substr , which essentially breaks the line depending on the position.
$string = 'this.is.a.sample'; $last_section = substr($string, (strrchr($string, '-') + 1)); echo $last_section; // 'sample'
Note the +1 result of strrchr ; this is because strrchr returns the index of the string in the string (starting at position 0), so the true position always contains 1 character.
source share