Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

replace &

by sandy1028 (Sexton)
on Jun 04, 2009 at 06:37 UTC ( #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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://768304]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2023-09-24 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?