The replacing of the html code for the ampersand symbol should be done last, not first, to avoid accidentally triggering one of the other substitutions.