$value =~ s/&(amp|quot)(?!;)/&$1;/g; # fix & and " without semi-colon $value =~ s/&(nbsp)(?!;)/ /g; # fix   without trailing semi-colon $value =~ s/&(#\d+)(?!;)/&$1;/g; # fix &# without trailing semi-colon $value =~ s/&(?!(amp|#\d+;|quot))/&/g; # convert any ampersands that are not an entity to & $value =~ s//>/g; # sanitze binary chars $value =~ tr/\x91\x92\x93\x94\x96\x97/''""\-\-/; $value =~ s/\x85/.../sg; $value =~ tr/\x80-\x9F//d;