Correct HTML Entity Encoding

| Comments (0)

[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

About this Entry

This page contains a single entry by kev published on February 27, 2008 4:08 PM.

Flickr Song Chart was the previous entry in this blog.

Lost is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.