How can you prevent double coding of html objects when they are allowed in the input

How can I prevent duplicate encoding of html entities or fix them programmatically?

I am using the encode () function from the HTML :: Entities module to encode HTML objects in user input. The problem here is that we also allow users to enter HTML objects directly, and these entities end in double coding.

For example, a user may enter:

Stackoverflow & Perl = Awesome…

It ends with an encoding

Stackoverflow & Perl = Awesome…

This is displayed in the browser as

Stackoverflow & Perl = Awesome…

We want this to display as

Stackoverflow & Perl = Awesome...

Is there any way to prevent this double encoding? Or is there a module or piece of code that can easily fix these double-coding problems?

Any help is much appreciated!

+4
source share
3 answers

You can decode the string first:

 my $input = from_user(); my $encoded = encode_entities( decode_entities $input ); 
+6
source

There is a very simple way to avoid this:

  • Delete all objects as you type (translate them to Unicode)
  • Encode in essence again at the output stage.
+4
source

Consider saving the call until encode() before getting the value to display, and not before saving it. If you agree on your search engine, the extra data in your database is probably not worth the trouble.

Edit

After re-reading your question, I understand that now my answer does not completely fix the problem, since calling encode() later will have the same results. Not knowing the alternative yourself, this may not be very useful, but you may want to find a more suitable encoding method that respects existing characters.

+1
source

Source: https://habr.com/ru/post/1306430/


All Articles