You might give an example of what you are trying to do. It's
not clear to me why " would occur inside html
tags. Generally that construction would appear outside such
tags in the text of the page in order to cause display of actual quotes. Inside tag brackets, actual quotes should be appropriate.
chas
(Update: If you are trying to correct an error, a substitution would likely achieve what you want. If you've tried this, you might exhibit your code attempts.)
Well I was building a script that did exactly that changing the double quotes in an HTML document to " But the side effect changes even the double quote inside the tags. This doesn't work for me. I then just tried to change them back to double quots but I could get that to work for me either. As you might have guessed I'm very new to this. But I know from the little I've been working on I can do this I'm just not sure how.
Thanks
and now your problem is solved. Well, unless you have JavaScript in the HTML, in which case you have another world of grief to deal with, though you can start with:
use HTML::TokeParser::Simple;
# time passes...
my $parser = HTML::TokeParser::Simple->new( string => $original_html )
+;
while (my $token = $parser->get_token) {
$token->rewrite_tag;
if ($token->is_text) {
my $t = $token->as_is;
$t =~ s/"/"e;/g;
print $t;
}
elsif ($token->is_start_tag("script")) {
# Embedded JavaScript, do not mess up!
print $token->as_is;
while ($token = $parser->get_token) {
print $token->as_is;
if ($token->is_tag("script") and not $token->is_start_tag) {
last; # done with JavaScript
}
}
}
else {
print $token->as_is;
}
}
I had to do the same thing a couple of days ago. Here is my HTML::Parser solution (to complement tilly's HTML::TokeParser version above). It encodes any special characters found in the text portion of an HTML doc.
use HTML::Parser;
use HTML::Entities;
my $html = '<div align="center">Your "HTML" page goes here</div>';
my $enc = '';
my $p = HTML::Parser->new(
unbroken_text => 1,
default_h => [ sub { $enc .= join('', @_) }, "text" ],
text_h => [ sub { $enc .= HTML::Entities::encode_entities($_[0]) },
+"text" ],
);
$p->parse($html);
print $encoded;