Typist
view release on metacpan or search on metacpan
lib/Typist/Util/String.pm view on Meta::CPAN
sub decode_url {
my ($str) = @_;
$str =~ s!%([0-9a-fA-F][0-9a-fA-F])!pack("H*",$1)!eg;
$str;
}
{
my $Have_Entities = eval 'use HTML::Entities; 1' ? 1 : 0;
my $NoHTMLEntities = 1; # hard coded. make switch? purpose?
sub encode_html {
my ($html, $can_double_encode) = @_;
return '' unless defined $html;
$html =~ tr!\cM!!d;
if ($Have_Entities && !$NoHTMLEntities) {
$html = HTML::Entities::encode_entities($html);
} else {
if ($can_double_encode) {
$html =~ s!&!&!g;
} else {
## Encode any & not followed by something that looks like
## an entity, numeric or otherwise.
$html =~ s/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w{1,8});)/&/g;
}
$html =~ s!"!"!g; #"
$html =~ s!<!<!g;
$html =~ s!>!>!g;
}
$html;
}
sub decode_html {
my ($html) = @_;
return '' unless defined $html;
$html =~ tr!\cM!!d;
if ($Have_Entities && !$NoHTMLEntities) {
$html = HTML::Entities::decode_entities($html);
} else {
$html =~ s!"!"!g; #"
$html =~ s!<!<!g;
$html =~ s!>!>!g;
$html =~ s!&!&!g;
}
$html;
}
}
{
my %Map = (
'&' => '&',
'"' => '"',
'<' => '<',
'>' => '>',
'\'' => '''
);
my %Map_Decode = reverse %Map;
my $RE = join '|', keys %Map;
my $RE_D = join '|', keys %Map_Decode;
sub encode_xml {
my ($str, $nocdata) = @_;
return '' unless defined $str;
if (
!$nocdata
&& $str =~ m/
<[^>]+> ## HTML markup
| ## or
&(?:(?!(\#([0-9]+)|\#x([0-9a-fA-F]+))).*?);
## something that looks like an HTML entity.
/x
) {
## If ]]> exists in the string, encode the > to >.
$str =~ s/]]>/]]>/g;
$str = '<![CDATA[' . $str . ']]>';
} else {
$str =~ s!($RE)!$Map{$1}!g;
}
$str;
}
sub decode_xml {
my ($str) = @_;
return '' unless defined $str;
if ($str =~ s/<!\[CDATA\[(.*?)]]>/$1/g) {
## Decode encoded ]]>
$str =~ s/]]&(gt|#62);/]]>/g;
} else {
$str =~ s!($RE_D)!$Map_Decode{$1}!g;
}
$str;
}
}
sub remove_html {
my ($text) = @_;
return $text if !defined $text; # suppress warnings
$text =~ s!<[^>]+>!!gs;
$text =~ s!<!<!gs;
$text;
}
1;
=head1 NAME
Typist::Util::String - Utility methods for string manipulation
=head1 METHODS
=over
=item decode_html
=item decode_xml
=item remove_html
=item encode_html
=item encode_xml
=item encode_js
=item encode_php
( run in 2.919 seconds using v1.01-cache-2.11-cpan-2398b32b56e )