Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: replace &

by wfsp (Abbot)
on Jun 04, 2009 at 07:02 UTC ( [id://768314]=note: print w/replies, xml ) Need Help??

in reply to replace &

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 HTML--- string - <© TVS& +gt;"; my $decoded = decode_entities($str); my $encoded = encode_entities($decoded); print $encoded; __DATA__ TEST&TEST;A&E&an HTML--- string - <© TVS +>
TEST&TEST;A&E&an HTML--- string - <© TVS>

Replies are listed 'Best First'.
Re^2: replace &
by sandy1028 (Sexton) on Jun 04, 2009 at 07:26 UTC
    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

        If a string and the HTML::Entities-decoded result of the string are the same, it is no HTML entity.

        To determine whether a string contains an HTML entity, you will be concerned with some information that may not be available by inspecting the string contents: the version of HTML, the character set of the HTML document, the character encoding of the string and possibly even the transfer encoding.

        This information might be available from the context in which you obtained the string. For example, if you received it from a web server via HTTP protocol, these attributes might have been specified in the HTTP protocol headers. If not, you may have to make some assumptions, which may or may not be correct. If you make incorrect assumptions, you results may not be satisfactory.

        For example, the World Wide Web Consortium publishes a full list of character references for HTML 4.01.

        Interpreting HTML documents is complicated by the fact that the character encoding can be specified for some elements in an HTML document, at least with HTML 4.01, so you may have to deal with multiple encodings within the same document. Also, the HTML 4.01 specification has this rather cryptic statement: Character references within comments have no special meaning; they are comment data only. So you might also be concerned when checking your string, to know whether it includes or is from the context of a comment.

        Your task may be further complicated by the need to deal with documents which were written to be consistent with some version of some popular browser rather than with any of the HTML or other specifications.

        All these difficulties aside, if you assume an HTML version and character encoding then you can search for any of the character entities defined in the HTML specification.

        Based on the information you have provided thus far, I would say that the solution provided by wspf is a good start and you should use it until you have some particular case for which it does not work.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://768314]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-21 23:45 GMT
Find Nodes?
    Voting Booth?

    No recent polls found