Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

replace &

by sandy1028 (Sexton)
on Jun 04, 2009 at 06:37 UTC ( [id://768304] : perlquestion . print w/replies, xml ) Need Help??

sandy1028 has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl; use HTML::Entities; $to_decode = "TEST&TEST;A&E&an HTML--- string - <© T +VS>"; #$decoded = decode_entities($to_decode); $encoded = encode_entities($to_decode); print "To decode --> $to_decode\n"; print "Encoded data --> $encoded\n";
output is :
To decode --> TEST&TEST;A&E&an HTML--- string - <© T +VS> Encoded data --> TEST&TEST;A&E&an HTML---&nbs +p;string - <© TVS>
the expected output is TEST&TEST;A&E&an HTML--- string - <© TVS> Here all the & is replacing with &. If it is HTML entity I have to write a code which will not repalce it. Please help me how to proceed further

Replies are listed 'Best First'.
Re: replace &
by wfsp (Abbot) on Jun 04, 2009 at 07:02 UTC
    I think you need to decode first and then encode.
    #! /usr/bin/perl use strict; use warnings; use HTML::Entities; my $str = "TEST&TEST;A&E&an&nbsp;HTML---&nbsp;string - &lt;&copy; TVS& +gt;"; my $decoded = decode_entities($str); my $encoded = encode_entities($decoded); print $encoded; __DATA__ TEST&amp;TEST;A&amp;E&amp;an&nbsp;HTML---&nbsp;string - &lt;&copy; TVS +&gt;
    TEST&TEST;A&E&an HTML--- string - <© TVS>
      If it is a HTML entities, it should not encode or decode. The string should be as it is. The output should see like this TEST&TEST;A&E&an HTML--- string - <© TVS>
        If it is a HTML entities, it should not encode or decode.

        Where did you get that idea?

        The string should be as it is. The output should see like this...

        I'm looking very carefully, and the string is exactly like that. If that isn't what you expect, post a hexdump of what you expect.

        How to check whether the string is a HTML entity in perl? Can any one tell me
Re: replace &
by Anonymous Monk on Jun 04, 2009 at 06:50 UTC