You are viewing a plain text version of this content. The canonical link for it is here.
Posted to asp@perl.apache.org by John Drago <jd...@e-commedia.net> on 2004/03/02 17:28:12 UTC

HTMLEn/Decode replacements

Hello,

I needed to make some changes to the Apache::ASP::Server::HTMLEn/Decode
subroutines so they would suit a particular purpose in a project I am
working on.

I am putting this code out here for what it's worth.  These work as
drop-in replacements for the subroutines by the same name in
Apache::ASP::Server.pm

Thanks.

<code>

#=======================================================================
======
# Contributed by John Drago <c0...@hotmail.com>
#=======================================================================
======
sub HTMLDecode
{
  my $self = shift if scalar(@_) > 1;
  my $str = shift;
  while( $str =~ /(&#(\d+);)/ )
  {
    my $whole = $1;
    my $char = $2;
    $char = chr($2);
    $str =~ s/$whole/$char/g;
  }# end while()
  return $str;
}# end htmlDecode()


#=======================================================================
======
# Contributed by John Drago <c0...@hotmail.com>
#=======================================================================
======
sub HTMLEncode
{
  my $self = shift if scalar(@_) > 1;
  my $str = shift;
  return '' unless defined $str;
  my $toEncode = ref($str) ? $str : \$str;
  my $char;
  $$toEncode =~ s/$1/"&#" . ord($char) . ";"/eg
    while ($char) = (
      $$toEncode =~ /
        ([^A-Z
           a-z
           0-9
           \~`!@\#\$%\^&\*\(\)\-_\+\=\{\}\[\]\:;'"\<,\>\.\?\\\/
           \s
        ])/x );
  if( ref($str) ){ return $toEncode }else{ return $$toEncode };
}# end htmlEncode()

</code>



---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org


RE: HTMLEn/Decode replacements

Posted by John Drago <jd...@e-commedia.net>.
Hi Josh,

I took your advice about the optimization and now have the following:
(It *is* tested and works).


#=============================================================
sub HTMLDecode
{
  my $self = shift if scalar(@_) > 1;
  my $str = shift;
  $str =~ s/&#(\d+);/chr($1)/eg;
  return $str;
}# end htmlDecode()


#=============================================================
sub HTMLEncode
{
  my $self = shift if scalar(@_) > 1;
  my $str = shift;
  return '' unless defined $str;
  my $toEncode = ref($str) ? $str : \$str;
    $$toEncode =~ s/(
      [^A-Z
        a-z
        0-9
        \~`!@\#\$%\^&\*\(\)\-_\+\=\{\}\[\]\:;'"\<,\>\.\?\\\s\/])/
      "&#" . ord($1) . ";"/egsx;
  if( ref($str) ){ return $toEncode }else{ return $$toEncode };
}# end htmlEncode()


_______________________________________________________________

John Drago | Chief Architect of Software Development
E-com Media Group, Inc. [www.e-commedia.com] 
office
::
 303.790.7940 x25
email
::
 jdrago@e-commedia.com



E - b u s i n e s s   w i t h   D i m e n s i o n TM


| -----Original Message-----
| From: Josh Chamas [mailto:josh@chamas.com]
| Sent: Tuesday, March 02, 2004 6:01 PM
| To: John Drago
| Cc: asp@perl.apache.org
| Subject: Re: HTMLEn/Decode replacements
| 
| John Drago wrote:
| > Hello,
| >
| > I needed to make some changes to the
Apache::ASP::Server::HTMLEn/Decode
| > subroutines so they would suit a particular purpose in a project I
am
| > working on.
| >
| > I am putting this code out here for what it's worth.  These work as
| > drop-in replacements for the subroutines by the same name in
| > Apache::ASP::Server.pm
| >
| 
| Hi John,
| 
| In considering adding this to the Apache::ASP code, I am wondering
| under what circumstances you would want to do this?  For possible
| inclusion,
| this might be something like HTMLEncodeEntities() or some such...
| 
| Also, if it does not make it in, you might consider subclassing
| Apache::ASP::Server,
| so you do not have to modify the base class itself.  As an
optimization
| for
| large conversions, you might lose the while() and:
| 
|     # not tested!
|     $$toEncode =~ s/[^A-Za-z0-9\~`!@\#\$%\^&\*\(\)\-
| _\+\=\{\}\[\]\:;'"\<,\>\.\?\\\/\s]/"&#" . ord($char) . ";"/egs;
| 
| Regards,
| 
| Josh
| 
|
________________________________________________________________________
| Josh Chamas, Founder    | NodeWorks - http://www.nodeworks.com
| Chamas Enterprises Inc. | NodeWorks Directory -
http://dir.nodeworks.com
| http://www.chamas.com   | Apache::ASP - http://www.apache-asp.org
| 
| 
| 
| ---------------------------------------------------------------------
| To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
| For additional commands, e-mail: asp-help@perl.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org


Re: HTMLEn/Decode replacements

Posted by Josh Chamas <jo...@chamas.com>.
John Drago wrote:
> Hello,
> 
> I needed to make some changes to the Apache::ASP::Server::HTMLEn/Decode
> subroutines so they would suit a particular purpose in a project I am
> working on.
> 
> I am putting this code out here for what it's worth.  These work as
> drop-in replacements for the subroutines by the same name in
> Apache::ASP::Server.pm
> 

Hi John,

In considering adding this to the Apache::ASP code, I am wondering
under what circumstances you would want to do this?  For possible inclusion,
this might be something like HTMLEncodeEntities() or some such...

Also, if it does not make it in, you might consider subclassing Apache::ASP::Server,
so you do not have to modify the base class itself.  As an optimization for
large conversions, you might lose the while() and:

    # not tested!
    $$toEncode =~ s/[^A-Za-z0-9\~`!@\#\$%\^&\*\(\)\-_\+\=\{\}\[\]\:;'"\<,\>\.\?\\\/\s]/"&#" . ord($char) . ";"/egs;

Regards,

Josh

________________________________________________________________________
Josh Chamas, Founder    | NodeWorks - http://www.nodeworks.com
Chamas Enterprises Inc. | NodeWorks Directory - http://dir.nodeworks.com
http://www.chamas.com   | Apache::ASP - http://www.apache-asp.org



---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org