Sometimes we run into databases that aren’t encoded properly. The following is just one method of fixing a very specific type of improper encoding: doubly encoded characters from Latin-1 to UTF-8.
All it’s really doing is enumerating the conversion pairs and running “update $1 set $2 = replace($2, ‘$3′, ‘$4′)” on them.