Pas mieux.
Une bonne solution (enfin ptet pas pour PHP mais bon...) c'est un langage de templating qui htmlencode tout par défaut, et te laisse marquer des chaînes/objets/impressions comme étant "safe" et à ne pas échapper.
Au delà de ça, si tu le fais avant l'affichage et que tu stockes le résultat tu vas te retrouver avec un bordel monstre de trucs pouvant ou non être encodés, et ça va te pêter à la gueule.
---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box by throwing away the limits imposed by overbearing genetic regulations? Isn't that a good thing?