Using str_replace()
<? print str_replace('{class}',$my_class, '<span class="{class}">A<span> <span class="{class}">B</span>'); ?>