If I understand you correctly, you want to only encode ampersands, try this:
echo str_replace('&', '&', $b);
[Edit]
If you're concerned about encoding instances of &
into &
, you could use:
echo preg_replace('/&[^(amp;)]/', '&', $b);
In this case Test & &
would be encoded as Test & $amp;