Non puoi utilizzre semplicemente htmlentities()?