First you said "... remove the space between and after the tags." Then later on you said "Remove the spaces in front of tags and after the tags." Those are two different things, but neither of them is such a good idea, frankly.

As ikegami said in his first reply, browsers always collapse consecutive white-space characters in html when rendering the text, so mucking with space characters in an html file is really unnecessary (from the point of view of someone reading the text in a browser).

If you think about html tags for a little bit, you'll notice that some of them (like <p> <table> <blockquote> <br/> and so on) are designed to control how browsers apply white-space when rendering html text (i.e. how they add spacing to enforce things like word separation, line breaks and indenting), while others (like <div> <span> <form> <input> and so on) have no impact on (do not add or control) spacing at all.

So, a process that blindly removes space characters that are adjacent to all tags is very likely to cause some damage to the text (from the point of view of someone trying to read it in a browser), because for some of those tags (div, span, form, etc), the space(s) next to the tag might be the only basis for separating two words that surround it.

If you think you have some other important reason for doing this (unrelated to what browsers normally do), it would help if you explain that. Depending on why you really want to do this, it's likely that you'll need to use one of the HTML parsing modules (e.g. HTML::Parser), and you'll need to be fairly careful about deciding which spaces to remove and which to keep.

(updated to add a couple words that were missing)


In reply to Re: space before and after by graff
in thread space before and after 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.