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-- &gt; 0) {
        $sploded[$len] = mb_substr($string, $len, 1); 
    }
    ksort($sploded);
    $len = mb_strlen($string);
    for ($i=0; $i &lt; $len; $i++) {
        if( strcmp($sploded[$i], "b") == 0){
            $temp = $sploded[$i];
            $sploded[$i] = $sploded[$i-1];
            $sploded[$i-1] = $temp;
        }
    } 
    return join('', $sploded);
}


Here I'm using mb_substr as my string contains unicode characters. The b is just an example.
In my production version I'm using this instead of "b"

html_entity_decode("&amp;#x0A3F;")


It is working as expected.
I'd appreciate if there could be a cleaner way to do it.
Also the strings passed to the function can contain as many as 10,000 - 20,000 characters.
So, I'd prefer a preg_replace function with regex to do the same.
You want to do a string replacement operation, so use preg_replace().  The key is to use backreferences in the replacement.  Also, since you seem to be interested in processing Unicode, be sure to use the u flag for the regex.

If, as you say, 'b' is just an example, then you can make it a default parameter.

function punjabi_alternate($string, $char='b') {
    return preg_replace("/(.)($char)/u", '\2\1', $string);
}