# Interchange all occurrences of a character and its predecessor

Given a string like "abcdbcfbc" , I want all the occurrences of 'b' to be interchanged with their previous character.
The output should be: "bacbdcbfc"

I've come up with a temporary solution:

function punjabi_alternate($string) {$len = mb_strlen($string);$sploded = array();
while($len-- > 0) {$sploded[$len] = mb_substr($string, $len, 1); } ksort($sploded);
$len = mb_strlen($string);
for ($i=0;$i < $len;$i++) {
if( strcmp($sploded[$i], "b") == 0){
$temp =$sploded[$i];$sploded[$i] =$sploded[$i-1];$sploded[$i-1] =$temp;
}
}