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

Re: html entity conversion... one liner?

Thread Previous | Thread Next
From:
Briac Pilpré
Date:
January 31, 2002 06:34
Subject:
Re: html entity conversion... one liner?
Message ID:
20020131143453.22684.qmail@onion.perl.org
On Thu, 31 Jan 2002 14:06:06 -0000, Michael Kavanagh
<Kavanagm@oup.co.uk> 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?

Here's a possible suboptimal one-liner approach:


#!/usr/bin/perl -w
use strict;

my $item = "<gah>buh</gah> zoh";

$item =~ s/([<>])/'&'. ($1 eq'<' ? 'l':'g') . 't;'/eg;

print $item;


__END__



-- 
briac
 << dynamic .sig on strike, we apologize for the inconvenience >>


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