Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Convert hex to HTML

by kcott (Archbishop)
on May 15, 2014 at 07:30 UTC ( [id://1086110]=note: print w/replies, xml ) Need Help??


in reply to Convert hex to HTML

I suggest you go back to whatever code generated this and fix it.

The text is littered with \u escapes. This escape has zero-length in the resulting string and causes the following character to be uppercased (see ucfirst); however, every following character is a zero which has no case!

While you could search for every occurence of /[0-9a-f]{4}/ (or, even more selectively, /00[3-7][0-9a-f]/) and convert them to characters, you're just as likely to be converting parts of telephone numbers.

You might get away with this but do so at your own risk!:

#!/usr/bin/env perl -l use strict; use warnings; my $text = " \u003cdiv class=\"ja-job-details\"\u003e \u003ch2 " . "class=\"title\"\u003eGlobal Service ..."; print "TEXT: $text"; (my $html = $text) =~ s/(00[3-7][0-9a-f])/pack 'H4', $1/eg; print "HTML: $html";

Output:

TEXT: 003cdiv class="ja-job-details"003e 003ch2 class="title"003eGlob +al Service ... HTML: <div class="ja-job-details"> <h2 class="title">Global Service . +..

-- Ken

Replies are listed 'Best First'.
Re^2: Convert hex to HTML
by Corion (Patriarch) on May 15, 2014 at 07:43 UTC

    If you think of the text as text in a text file, instead of text in Perl source code, then the \uXXXX escapes would suggest Javascript Unicode escapes, which are usually comprised of four letters.

    Adapting your regex to such text would then yield the same result as your other approach though.

    But as we lack further information, it's kinda hard to determine what was really meant.

      Yes, you could well be right about that.

      #!/usr/bin/env perl -l use strict; use warnings; use JSON; my $text = '{"html":" \u003cdiv class=\"ja-job-details\"\u003e \u003ch +2 ' . 'class=\"title\"\u003eGlobal Service ..."}'; print decode_json($text)->{html};

      Output:

      <div class="ja-job-details"> <h2 class="title">Global Service ...

      -- Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-04-24 00:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found