Correct HTML Entity Encoding
[Moozik: The Birthday Massacre - Falling Down]
This afternoon I tracked an entertaining bug down to the following code humorously commented as "encode nasty entities".
# encode nasty entities...
$string =~ s/&/&/;
Yipes. First off that code is not doing anything with any entity other than an ampersand. Secondly there's a missing /g modifier so that regex will only change the first ampersand found in the string and ignore any others. What happens when we encounter 'J&D Guns & Ammo'? Yuck.
That's not encoding, so don't do that. Use HTML::Entities instead and take the worry out.
$string = encode_entities($string);
And so ends my Perl public service announcement for today.
Leave a comment