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.
You can manually add a character to the translation table (char2entity hash).
@, 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.