Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7081019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:52:00+00:00 2026-05-28T06:52:00+00:00

I have a string where special characters like ! or or & or #

  • 0

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 &quot ;XYZ&quot ; this &#39 ;is&#39 ; a test &amp ; so on @" 

I tried

$str=HTML::Entities::encode_entities($str);

but it does a partial work the @ is not transformed in &#64 ;

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:52:01+00:00Added an answer on May 28, 2026 at 6:52 am

    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{<>&"'@} );
    
    1. The above adds @, which will be translated to &#64;.
    2. You then need to specify the characters you want to translate, if you don’t it uses <>&", so I added both @ and '. Notice, I didn’t have to add the ' to the translation table, because it’s already there by default.
    3. You don’t need to add ASCII characters (0-255) to the char2entity hash, since the module will do it automatically.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string where special characters like ' or or & (...) can
I have a string with special characters like this: äöüß& Now I want to
I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files
I have a string with special characters in Java. [^\\]*\\ I want to convert
I have a string which may hold special characters like: $ , ( ,
I have a string object with multiple characters and even special characters I am
I have certain strings which contain special characters so they can not be shared
I have string like this /c SomeText\MoreText Some Text\More Text\Lol SomeText I want to
I have string like \LESSING\root\cimv2:Win32_UserAccount.Domain=LESSING,Name=Admin How to convert it to LESSING\Admin using Framework?
I have string looking like this: 'Toy Story..(II) (1995)' I want to split the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.