Skip to content

Commit

Permalink
Added str_replace_last for Laravel 5.0 & 5.1 backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Nov 24, 2017
1 parent e1408c3 commit c2b07ed
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,24 @@ function str_after($subject, $search)
return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0];
}
}

if (!function_exists('str_replace_last')) {
/**
* Replace the last occurrence of a given value in the string.
*
* @param string $search
* @param string $replace
* @param string $subject
* @return string
*/
function str_replace_last($search, $replace, $subject)
{
$position = strrpos($subject, $search);

if ($position !== false) {
return substr_replace($subject, $replace, $position, strlen($search));
}

return $subject;
}
}

0 comments on commit c2b07ed

Please sign in to comment.