Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > "Escape" in perl

Reply
Thread Tools

"Escape" in perl

 
 
Bill H
Guest
Posts: n/a
 
      10-16-2008
I am using the following code to unescape html text that is coming
from flash:

sub unescape
{
my $text = shift;
$text =~ s/%(..)/pack("c",hex($1))/ge;
return($text);
}

for example it will take this text:

%3CFONT%20FACE%3D%22timesnewroman%22%20COLOR%3D%22 #000000%22%20SIZE%3D
%2220%22%3E%3CP%20ALIGN%3D%22CENTER%22%3EChapter%2 0Title%3C%2FP%3E%3C
%2FFONT%3E

and it will convert it to this:

<FONT FACE="timesnewroman" COLOR="#000000" SIZE="20"><P
ALIGN="CENTER">Chapter Title</P></FONT>

What I am trying to figure out is how to go the other way in perl,
convert the html to an escaped format. Any hints, clues, pointers
would be appreciated

Bill H
 
Reply With Quote
 
 
 
 
Tim Greer
Guest
Posts: n/a
 
      10-16-2008
Bill H wrote:

> I am using the following code to unescape html text that is coming
> from flash:
>
> sub unescape
> {
> my $text = shift;
> $text =~ s/%(..)/pack("c",hex($1))/ge;
> return($text);
> }
>
> for example it will take this text:
>
> %3CFONT%20FACE%3D%22timesnewroman%22%20COLOR%3D%22 #000000%22%20SIZE%3D
> %2220%22%3E%3CP%20ALIGN%3D%22CENTER%22%3EChapter%2 0Title%3C%2FP%3E%3C
> %2FFONT%3E
>
> and it will convert it to this:
>
> <FONT FACE="timesnewroman" COLOR="#000000" SIZE="20"><P
> ALIGN="CENTER">Chapter Title</P></FONT>
>
> What I am trying to figure out is how to go the other way in perl,
> convert the html to an escaped format. Any hints, clues, pointers
> would be appreciated
>
> Bill H


Just don't run the input through the unescape sub routine (or have one
that does and another that doesn't (and do whatever you want with the
other))? Or am I misunderstanding your problem/question?
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
 
 
 
Bill H
Guest
Posts: n/a
 
      10-16-2008
On Oct 16, 6:45*pm, Tim Greer <(E-Mail Removed)> wrote:
> Bill H wrote:
> > I am using the following code to unescape html text that is coming
> > from flash:

>
> > sub unescape
> > {
> > * * my $text = shift;
> > * * $text =~ s/%(..)/pack("c",hex($1))/ge;
> > * * return($text);
> > }

>
> > for example it will take this text:

>
> > %3CFONT%20FACE%3D%22timesnewroman%22%20COLOR%3D%22 #000000%22%20SIZE%3D
> > %2220%22%3E%3CP%20ALIGN%3D%22CENTER%22%3EChapter%2 0Title%3C%2FP%3E%3C
> > %2FFONT%3E

>
> > and it will convert it to this:

>
> > <FONT FACE="timesnewroman" COLOR="#000000" SIZE="20"><P
> > ALIGN="CENTER">Chapter Title</P></FONT>

>
> > What I am trying to figure out is how to go the other way in perl,
> > convert the html to an escaped format. Any hints, clues, pointers
> > would be appreciated

>
> > Bill H

>
> Just don't run the input through the unescape sub routine (or have one
> that does and another that doesn't (and do whatever you want with the
> other))? *Or am I misunderstanding your problem/question?
> --
> Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
> Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
> and Custom Hosting. *24/7 support, 30 day guarantee, secure servers.
> Industry's most experienced staff! -- Web Hosting With Muscle!- Hide quoted text -
>
> - Show quoted text -


Tim

Actually the problem is that I want to use perl to make a lot of
default "escaped" html for flash. Up till now I have been just using
flash to do it, but now I have about 60 templates that I need to
create and know there has to be a better way then code actionscript,
run flash, cut / paste escaped stuff into perl code, run perl to
create template, repeat.

I was thinking of using HTML::Entities, but I can't install it on the
server I am using.

Bill H
 
Reply With Quote
 
Bill H
Guest
Posts: n/a
 
      10-16-2008
On Oct 16, 7:05*pm, Bill H <(E-Mail Removed)> wrote:
> On Oct 16, 6:45*pm, Tim Greer <(E-Mail Removed)> wrote:
>
>
>
>
>
> > Bill H wrote:
> > > I am using the following code to unescape html text that is coming
> > > from flash:

>
> > > sub unescape
> > > {
> > > * * my $text = shift;
> > > * * $text =~ s/%(..)/pack("c",hex($1))/ge;
> > > * * return($text);
> > > }

>
> > > for example it will take this text:

>
> > > %3CFONT%20FACE%3D%22timesnewroman%22%20COLOR%3D%22 #000000%22%20SIZE%3D
> > > %2220%22%3E%3CP%20ALIGN%3D%22CENTER%22%3EChapter%2 0Title%3C%2FP%3E%3C
> > > %2FFONT%3E

>
> > > and it will convert it to this:

>
> > > <FONT FACE="timesnewroman" COLOR="#000000" SIZE="20"><P
> > > ALIGN="CENTER">Chapter Title</P></FONT>

>
> > > What I am trying to figure out is how to go the other way in perl,
> > > convert the html to an escaped format. Any hints, clues, pointers
> > > would be appreciated

>
> > > Bill H

>
> > Just don't run the input through the unescape sub routine (or have one
> > that does and another that doesn't (and do whatever you want with the
> > other))? *Or am I misunderstanding your problem/question?
> > --
> > Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
> > Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
> > and Custom Hosting. *24/7 support, 30 day guarantee, secure servers.
> > Industry's most experienced staff! -- Web Hosting With Muscle!- Hide quoted text -

>
> > - Show quoted text -

>
> Tim
>
> Actually the problem is that I want to use perl to make a lot of
> default "escaped" html for flash. Up till now I have been just using
> flash to do it, but now I have about 60 templates that I need to
> create and know there has to be a better way then code actionscript,
> run flash, cut / paste escaped stuff into perl code, run perl to
> create template, repeat.
>
> I was thinking of using HTML::Entities, but I can't install it on the
> server I am using.
>
> Bill H- Hide quoted text -
>
> - Show quoted text -


The other thought I had, the brute force approach, is to just escape
every character in the string (ie convert each character to a %??). I
suppose I could get a little creative and not touch any numbers or
letters, just everything below ascii 48, above ascii 57 and below
ascii 65. Above ascii 90 and below ascii 97, and above ascii 122.
Wonder if there is a regex that could do this for me?

Bill H
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      10-16-2008
Bill H <(E-Mail Removed)> wrote:

> I was thinking of using HTML::Entities, but I can't install it on the
> server I am using.



Why not?

That is, what problem did you experience when you tried it?


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Petr Vileta \(fidokomik\)
Guest
Posts: n/a
 
      10-16-2008
Bill H wrote:
> I was thinking of using HTML::Entities, but I can't install it on the
> server I am using.
>


HTML::Entities is the right way. When you have not a rights to install it on
server then you can try 2 ways.
1) Ask server admin or tech. support for installing this module
2) download HTML::Entities and HTML:arser modules, place these modules to some
directory on server and write some like this to your code:

require "/full_path_to_module/HTML/Entities.pm";
require "/full_path_to_module/HTML/Parser.pm";

I think these modules have not other dependencies then Entities to Parser and
Parser to Entities.

--
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail.
Send me your mail from another non-spammer site please.)
Please reply to <petr AT practisoft DOT cz>

 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      10-17-2008
On Thu, 16 Oct 2008 15:21:21 -0700 (PDT), Bill H <(E-Mail Removed)> wrote:

>I am using the following code to unescape html text that is coming
>from flash:
>
>sub unescape
>{
> my $text = shift;
> $text =~ s/%(..)/pack("c",hex($1))/ge;
> return($text);
>}
>
>for example it will take this text:
>
>%3CFONT%20FACE%3D%22timesnewroman%22%20COLOR%3D%2 2#000000%22%20SIZE%3D
>%2220%22%3E%3CP%20ALIGN%3D%22CENTER%22%3EChapter% 20Title%3C%2FP%3E%3C
>%2FFONT%3E
>
>and it will convert it to this:
>
><FONT FACE="timesnewroman" COLOR="#000000" SIZE="20"><P
>ALIGN="CENTER">Chapter Title</P></FONT>
>
>What I am trying to figure out is how to go the other way in perl,
>convert the html to an escaped format. Any hints, clues, pointers
>would be appreciated
>
>Bill H


This might be one way:

sub escape
{
my $text = shift;
$text =~ s/([<>= "#])/'%'.uc sprintf("%x", ord($1))/ge;
return($text);
}


 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      10-17-2008
Bill H wrote:

> On Oct 16, 6:45*pm, Tim Greer <(E-Mail Removed)> wrote:
>> Bill H wrote:
>> > I am using the following code to unescape html text that is coming
>> > from flash:

>>
>> > sub unescape
>> > {
>> > my $text = shift;
>> > $text =~ s/%(..)/pack("c",hex($1))/ge;
>> > return($text);
>> > }

>>
>> > for example it will take this text:

>>
>> >

%3CFONT%20FACE%3D%22timesnewroman%22%20COLOR%3D%22 #000000%22%20SIZE%3D
>> >

%2220%22%3E%3CP%20ALIGN%3D%22CENTER%22%3EChapter%2 0Title%3C%2FP%3E%3C
>> > %2FFONT%3E

>>
>> > and it will convert it to this:

>>
>> > <FONT FACE="timesnewroman" COLOR="#000000" SIZE="20"><P
>> > ALIGN="CENTER">Chapter Title</P></FONT>

>>
>> > What I am trying to figure out is how to go the other way in perl,
>> > convert the html to an escaped format. Any hints, clues, pointers
>> > would be appreciated

>>
>> > Bill H

>>
>> Just don't run the input through the unescape sub routine (or have
>> one that does and another that doesn't (and do whatever you want with
>> the other))? *Or am I misunderstanding your problem/question?
>> --

....

>>
>> - Show quoted text -

>
> Tim
>
> Actually the problem is that I want to use perl to make a lot of
> default "escaped" html for flash. Up till now I have been just using
> flash to do it, but now I have about 60 templates that I need to
> create and know there has to be a better way then code actionscript,
> run flash, cut / paste escaped stuff into perl code, run perl to
> create template, repeat.
>
> I was thinking of using HTML::Entities, but I can't install it on the
> server I am using.
>
> Bill H


That should work. Did you try installing the module's files in your own
account's "lib" directory (create one and call the module there if you
need to)?
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      10-17-2008
On Fri, 17 Oct 2008 00:18:21 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>On Thu, 16 Oct 2008 15:21:21 -0700 (PDT), Bill H <(E-Mail Removed)> wrote:
>
>>I am using the following code to unescape html text that is coming
>>from flash:
>>
>>sub unescape
>>{
>> my $text = shift;
>> $text =~ s/%(..)/pack("c",hex($1))/ge;
>> return($text);
>>}
>>
>>for example it will take this text:
>>
>>%3CFONT%20FACE%3D%22timesnewroman%22%20COLOR%3D% 22#000000%22%20SIZE%3D
>>%2220%22%3E%3CP%20ALIGN%3D%22CENTER%22%3EChapter %20Title%3C%2FP%3E%3C
>>%2FFONT%3E
>>
>>and it will convert it to this:
>>
>><FONT FACE="timesnewroman" COLOR="#000000" SIZE="20"><P
>>ALIGN="CENTER">Chapter Title</P></FONT>
>>
>>What I am trying to figure out is how to go the other way in perl,
>>convert the html to an escaped format. Any hints, clues, pointers
>>would be appreciated
>>
>>Bill H

>
>This might be one way:
>
>sub escape
>{
> my $text = shift;
> $text =~ s/([<>= "#])/'%'.uc sprintf("%x", ord($1))/ge;
> return($text);
>}
>


Yeah, this seems to work. Try all except alpha-numeric and newline.

sln


####################
# Esc_some_html.pl
####################
use strict;
use warnings;

# Try all except alpha-numeric and newline
# --------------------------------------------
my $htmlchrs = '[^\w\n]'; # or '[<>= "#]'

my $str_escaped = '
%3CFONT%20FACE%3D%22timesnewroman%22%20COLOR%3D%22 #000000%22%20SIZE%3D
%2220%22%3E%3CP%20ALIGN%3D%22CENTER%22%3EChapter%2 0Title%3C%2FP%3E%3C
%2FFONT%3E';


my $str_normal = unescape( $str_escaped);

print "$str_normal\n\n";

print escape( $str_normal)."\n";

sub unescape
{
my $text = shift;
$text =~ s/%(..)/pack("c",hex($1))/ge;
return($text);
}

sub escape
{
my $text = shift;
$text =~ s/($htmlchrs)/'%'.uc sprintf("%02x", ord($1))/ge;
return($text);
}

__END__

output:

<FONT FACE="timesnewroman" COLOR="#000000" SIZE=
"20"><P ALIGN="CENTER">Chapter Title</P><
/FONT>


%3CFONT%20FACE%3D%22timesnewroman%22%20COLOR%3D%22 %23000000%22%20SIZE%3D
%2220%22%3E%3CP%20ALIGN%3D%22CENTER%22%3EChapter%2 0Title%3C%2FP%3E%3C
%2FFONT%3E



 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      10-17-2008
Bill H <(E-Mail Removed)> wrote:

> >
> > > for example it will take this text:

> >
> > > %3CFONT%20FACE%3D%22timesnewroman%22%20COLOR%3D%22 #000000%22%20SIZE%3
> > > D
> > > %2220%22%3E%3CP%20ALIGN%3D%22CENTER%22%3EChapter%2 0Title%3C%2FP%3E%3C
> > > %2FFONT%3E

> >
> > > and it will convert it to this:

> >
> > > <FONT FACE=3D"timesnewroman" COLOR=3D"#000000" SIZE=3D"20"><P
> > > ALIGN=3D"CENTER">Chapter Title</P></FONT>

....
>
> I was thinking of using HTML::Entities,


What you want is not HTML entity escaping (&lt, but URI escaping
(%3C). See URI::Escape.

> but I can't install it on the
> server I am using.


URI::Escape looks like it is pure Perl. If you can run Perl modules of
your own creation, then you can install it, as it is just anther Perl
module.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 04-04-2011 10:00 PM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 02-27-2011 11:00 PM
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments