puoi usare direttamente la funzione htmlentities in fase di output che ti sostituisce i caratteri particolari con i corrispettivi codici html