What is the function in PHP that can be used to replace all occurrences of a character in a string, except for the last one?

To replace all occurrences of a character in a string in PHP, except for the last one, you can use a combination of functions like `substr_count()` to count the occurrences, `strrpos()` to find the position of the last occurrence, and `str_replace()` to perform the replacements. By determining the position of the last occurrence, you can replace all other occurrences before it.

$string = "hello world hello";
$char = "l";
$count = substr_count($string, $char);
$lastPos = strrpos($string, $char);

$result = str_replace($char, "", substr($string, 0, $lastPos)) . substr($string, $lastPos);

echo $result;