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

Re: html entity conversion... one liner?

Thread Previous | Thread Next
From:
John W. Krahn
Date:
January 31, 2002 06:44
Subject:
Re: html entity conversion... one liner?
Message ID:
3C595859.D8115273@acm.org
Michael Kavanagh wrote:
> 
> I thought it would be good to be able to do this:
> 
> $item = "<blah>";
> $item  =~ tr/<>/(&lt;)(&gt;)/;

tr/// will translate the '<' to a '(' and the '>' to a '&'.

> 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?


$item =~ s{([<>])} {$1 eq '<' ? '&lt;' : '&gt;'}ge;


John
-- 
use Perl;
program
fulfillment

Thread Previous | Thread Next


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