develooper Front page | perl.beginners | Postings from January 2002

Re: html entity conversion... one liner?

Thread Previous
From:
Chas Owens
Date:
January 31, 2002 06:33
Subject:
Re: html entity conversion... one liner?
Message ID:
1012487634.1888.28.camel@tert.icallinc.com
On Thu, 2002-01-31 at 09:06, KAVANAGH, Michael wrote:
> I thought it would be good to be able to do this:
> 
> $item = "<blah>";
> $item  =~ tr/<>/(&lt;)(&gt;)/;
> 
> to convert those <> symbols to their entity references.
> however, the tr operator doesn't seem to like using () to group... any
> comments on how to make this operation in to a one-liner?
> 
> Thanks
> Mike
> 
> 

I could be wrong, but I thought tr/// and y/// were only for
characters.  I think you might want:

my %entity = (
        '<' => '&lt;',
        '>' => '&gt;',
        '&' => '&amp;',
);

my $item = "<blah>";


$item =~ s/([<>&])/$entity{$1}/ge;


-- 
Today is Sweetmorn the 31st day of Chaos in the YOLD 3168


Missle Address: 33:48:3.521N  84:23:34.786W


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About