I'm trying to s/// on the plaintext in a variable containing HTML. If naively I do $html =~ s/PERL/Perl/g, lots of links and HTML elements become broken. I just want the text to be replaced.

I looked on CPAN and found HTML::FormatText, which converts the HTML to plaintext. But it won't convert back to HTML after I do the substitution. Can I use HTML::Parser, HTML::TokeParser, HTML::TokeParser::Simple, or HTML::TreeBuilder to identify the plaintext, manipulate it how I see fit, and reassemble the structure into the original HTML, with plaintext modifications? If so, how?

I looked at Sean M. Burke's article in TPJ, and it mentions the as_text() method, but I'm at wits end how to put the modified text back in the HTML document. Can someone provide an example? I just want to apply a regex to the plain text portion of $html, how can I do this? Thanks in advance, -jc


In reply to Manipulating plaintext within HTML by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.