Skip to content

Instantly share code, notes, and snippets.

@axiak
Created August 24, 2011 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axiak/1168155 to your computer and use it in GitHub Desktop.
Save axiak/1168155 to your computer and use it in GitHub Desktop.
sub js_encode {
my $text = shift;
my @js_entities = ("\\", "\\\\", '&', '\&', '"', '\"', '<', '\<', '>', '\>', "'", "\'");
# This was what was there before:
#my @js_entities = ('&', '\&', '"', '\"', '<', '\<', '>', '\>', "'", "\'");
my ($char, $js_char);
for (my $i = 0; $i < scalar @js_entities; $i = $i + 2) {
$text =~ s/$js_entities[$i]/$js_entities[$i+1]/g;
}
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment