Someone pointed out a strange bug I was able to fix by changing $r = str_replace("{$x}", '~*~', $r); to $r = str_replace('{'.$x.'}', '~*~', $r); Evidently the string replacement matched on patterns like {emailbody} when $x=email. It seems it shouldn't match. Can anyone explain why? Cheers, Dan