title: encode special character in html entities in perl
body: <p>I have a string where special characters like <code>!</code> or <code>"</code> or <code>&amp;</code> or <code>#</code> or <code>@</code>, ... can appear. How can I convert in the string</p>
  
  <pre><code>str = " Hello "XYZ" this 'is' a test &amp; so *n @."
  </code></pre>
  
  <p>automatically every special characters with their html entities, so that I get this:</p>
  
  <pre><code>str = " Hello &amp;quot ;XYZ&amp;quot ; this &amp;#39 ;is&amp;#39 ; a test &amp;amp ; so on @" 
  </code></pre>
  
  <p>I tried </p>
  
  <pre><code>$str=HTML::Entities::encode_entities($str);
  </code></pre>
  
  <p>but it does a partial work the <code>@</code> is not transformed in <code>&amp;#64</code> ;</p>
  
  <p><strong>SOLUTION:</strong></p>
  
  <p>1) with your help (Quentin and vol7ron) I came up with this solution(1)</p>
  
  <pre><code>$HTML::Entities::char2entity{'@'} = '&amp;#64;';
  $HTML::Entities::char2entity{'!'} = '&amp;#33;';
  $HTML::Entities::char2entity{'#'} = '&amp;#35;';
  $HTML::Entities::char2entity{'%'} = '&amp;#37;';
  $HTML::Entities::char2entity{'.'} = '&amp;#46;';
  $HTML::Entities::char2entity{'*'} = '&amp;#42;';
  $str=HTML::Entities::encode_entities($str, q{@"%'.&amp;#*$^!});
  </code></pre>
  
  <p>2) and I found a shorter(better) solution(2) found it <a href="http://www.perlmonks.org/?node_id=640603" rel="nofollow">here</a>: </p>
  
  <pre><code>$str=HTML::Entities::encode_entities($str, '\W');
  </code></pre>
  
  <p>the '\W' does the job </p>
  
  <p><strong>@von7ron</strong> with solution(1) you will need to specify the characters you want to translate as <strong>Quentin</strong> mentioned earlier even if they are on the translation table.</p>