How to Get a String before the Last Occurrence of a String or Character in Laravel
In this lesson, we are going to see how to get a string before the last occurrence of a string or character in Laravel, the beforeLast helper method is used to return a section of a string that appears before the last occurrence of the provided string.
The Str::beforeLast helper method
The beforeLast() method is used to return a section of a string before the last occurrence of a specific string.
use Illuminate\Support\Str;
$website = 'http://www.website.com/tech/posts';
$part = Str::beforeLast($website, '/');
echo $part;
// Returns "https://www.website.com/tech"