Reputation: 2153
I have a string where special characters like !
or "
or &
or #
or @
, ... can appear. How can I convert in the string
str = " Hello "XYZ" this 'is' a test & so *n @."
automatically every special characters with their html entities, so that I get this:
str = " Hello " ;XYZ" ; this ' ;is' ; a test & ; so on @"
I tried
$str=HTML::Entities::encode_entities($str);
but it does a partial work the @
is not transformed in @
;
SOLUTION:
1) with your help (Quentin and vol7ron) I came up with this solution(1)
$HTML::Entities::char2entity{'@'} = '@';
$HTML::Entities::char2entity{'!'} = '!';
$HTML::Entities::char2entity{'#'} = '#';
$HTML::Entities::char2entity{'%'} = '%';
$HTML::Entities::char2entity{'.'} = '.';
$HTML::Entities::char2entity{'*'} = '*';
$str=HTML::Entities::encode_entities($str, q{@"%'.&#*$^!});
2) and I found a shorter(better) solution(2) found it here:
$str=HTML::Entities::encode_entities($str, '\W');
the '\W' does the job
@von7ron with solution(1) you will need to specify the characters you want to translate as Quentin mentioned earlier even if they are on the translation table.
Upvotes: 4
Views: 9116
Reputation: 42099
You can manually add a character to the translation table (char2entity hash).
$HTML::Entities::char2entity{'@'} = '@';
my $str = q{ Hello "XYZ" this 'is' a test & so on @};
my $encoded = HTML::Entities::encode_entities( $str, q{<>&"'@} );
@
, which will be translated to @
. <>&"
, so I added both @
and '
. Notice, I didn't have to add the '
to the translation table, because it's already there by default.Note: Setting the char2entity for @
, was done as an example. The module automatically sets numerical entities for ASCII characters (0-255) that weren't found. You'd have to use it for unicode characters, though.
Upvotes: 2
Reputation: 3205
Cheap, dirty, and ugly, but works:
my %translations;
$translations{'"'} = '" ;';
$translations{'\''} = '' ;';
etc...
sub transform()
{
my $str = shift;
foreach my $character (keys(%translations))
{
$str =~ s/$character/$translations{$character}/g;
}
return $str;
}
Upvotes: -1
Reputation: 943537
@
isn't transformed because it isn't considered to be a "special character". It can be represented in ASCII and has no significant meaning in HTML.
You can expand the range of characters that are converted with the second argument to the function you are using, as described in the documentation.
Upvotes: 5