Sachin Kulkarni
Sachin Kulkarni

Reputation: 1705

HTML entities encoding entity numbers

I have a test string like :

my $input = "testing &test ₨";
my $output = HTML::Entities::encode_entities($str,"<>&\"'");

The desired output is

testing &amp;test &#8360;

But HTML::Entities::encode_entities is encoding this into

testing &amp;test &amp;#8360;

To summarize, I want the HTML::Entities to encode the "&" character only if it does not represent an HTML entity number.

Upvotes: 0

Views: 549

Answers (1)

Perleone
Perleone

Reputation: 4038

You need to decode the string first, then encode it:

#!/usr/bin/env perl
use strict; use warnings; use v5.10;
use HTML::Entities ();
my $input = "testing &test &#8360;";
$input = HTML::Entities::encode( HTML::Entities::decode( $input ) );
say $input;

The result is

testing &amp;test &#x20A8;

20A8 is the hexadecimal version of 8360.

Upvotes: 7

Related Questions