Skip to content

Instantly share code, notes, and snippets.

@kjbrum
Created January 21, 2016 22:43
Show Gist options
  • Save kjbrum/298d8a898df8f1c9230b to your computer and use it in GitHub Desktop.
Save kjbrum/298d8a898df8f1c9230b to your computer and use it in GitHub Desktop.
Replace the last occurrence of a string.
<?php
/**
* Replace the last occurrence of a string.
*
* @param string $search The string to search for
* @param string $replace The string to replace the search string
* @param string $subject The string to search and replace
* @return string The new string
*/
function str_replace_last( $search=null, $replace=null, $subject=null ) {
return strrev( implode( strrev( $replace ), explode( $search, strrev( $subject ), 2 ) ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment