Re: Help with HTML::Entities - decode_entities
by ikegami (Patriarch) on Jul 09, 2010 at 00:23 UTC
|
Even if it was possible to do what you want with HTML::Entities — and it looks like you can't — It would make most sense to just use
$mystring =~ s/;/;/g;
$mystring =~ s/&/&/g;
That said, decoding "&" without decoding all other entities creates garbage. For example
é
and
é
are clearly not equivalent, but they produce the same string under your requirements.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] |
Re: Help with HTML::Entities - decode_entities
by Your Mother (Archbishop) on Jul 08, 2010 at 23:46 UTC
|
I think the leading underscore is necessary--best not to second guess the documents when you're having problems--and it seems to do it in place so you have to check the variable, not the return from the function.
perl -MHTML::Entities -le '$s = "ÖH & HAÌ"; _decode_en
+tities($s, { amp => "&" }, 0); print $s'
--
ÖH & HAÌ
| [reply] [Watch: Dir/Any] [d/l] |
|
_decode_entities($mystring, { amp => "&", 59 => ";" }, 0);
print $mystring;
but when $mystring = "Fred's shoe", I'm still getting "Fred's shoe" output.
Time flies like an arrow. Fruit flies like a banana.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Help with HTML::Entities - decode_entities
by Anonymous Monk on Jul 08, 2010 at 23:49 UTC
|
#!/usr/bin/perl --
use strict;
use warnings;
use HTML::Entities;
use Test::More tests => 3;
my $str = q!amp & 59 ; quot "!;
is( decode_entities($str), 'amp & 59 ; quot "' );
is( join( '-', decode_entities( $str, $str ) ),
join( '-', 'amp & 59 ; quot "' , 'amp & 59 ; quot "' ) );
{
my $str = $str;
_decode_entities( $str, { amp => "&", 59 => ";" }, 0 );
is( $str, 'amp & 59 ; quot "' );
}
__END__
http://search.cpan.org/dist/HTML-Parser/MANIFESTt/entities.t Test encoding/decoding of entities
t/entities2.t Test _decode_entities()
How (Not) To Ask A Question | [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] |
|
$ perl -MTest::More=tests,2 -e"is( 1, 2 )"
1..2
not ok 1
# Failed test at -e line 1.
# got: '1'
# expected: '2'
# Looks like you planned 2 tests but ran 1.
# Looks like you failed 1 test of 1 run.
| [reply] [Watch: Dir/Any] [d/l] |
Re: Help with HTML::Entities - decode_entities
by punch_card_don (Curate) on Jul 09, 2010 at 00:27 UTC
|
For example, this test code:
#!/usr/bin/perl
use strict;
use warnings;
use HTML::Entities;
print "Content-type: text/html\n\n";
my $str = "Fred's shoe";
_decode_entities($str, { amp => "&", 59 => ";" }, 0);
print "<p>$str\n";
Outputs
<p>Fred's shoe
even though the docs say
_decode_entities( $string, \%entity2char, $expand_prefix )
This will in-place replace HTML entities in $string. The %entity2char hash must be provided. Named entities not found in the %entity2char hash are left alone.
Time flies like an arrow. Fruit flies like a banana.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
"'" is not a named entity. It's numerical.
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
Named entities not found in the %entity2char hash are left alone. Numeric entities are expanded unless their value overflow.
| [reply] [Watch: Dir/Any] |