In a recent project I had a need to replace the last occurrence of a character in a string with another string. While our first function works as intended, following are some others that we found on stackoverflow that also do as we intended. They're reproduced here for your benefit and as a reference for our team.
■ ■ ■
1
<?php
2
/*
3
Replace the last occurence of a charater (in a string) with PHP
4
http://www.beliefmedia.com/code/php-snippets/replace-last-occurrence-string
5
*/
6
7
function beliefmedia_str_lreplace($string, $find, $replace) {
8
9
10
return $string;
11
}
1
<?php
2
/*
3
Replace the last occurence of a charater (in a string) with PHP
4
http://www.beliefmedia.com/code/php-snippets/replace-last-occurrence-string
5
*/
6
7
function beliefmedia_str_lreplace($string, $find, $replace) {
8
9
if ($pos === false) return $string;
10
11
return $string;
12
}
1
<?php
2
/*
3
Replace the last occurence of a charater (in a string) with PHP
4
http://www.beliefmedia.com/code/php-snippets/replace-last-occurrence-string
5
*/
6
7
function beliefmedia_str_lreplace($string, $find, $replace) {
8
9
}
Usage
Example:
returns: cat, dog, frog, and cow
.