How to change text inside an HTML tag?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kent Brewster

    How to change text inside an HTML tag?

    I've got a (large, hairy, evil) database full of raw dumps from MySQL
    that look like this:

    <code>
    +----+----------+
    | k | t |
    +----+----------+
    | 1 | apple |
    | 2 | orange |
    | 3 | pear |
    +----+----------+
    3 rows in set (0.00 sec)
    </code>

    When displayed in HTML, the extra spaces don't show, leaving this:

    +----+----------+
    | k | t |
    +----+----------+
    | 1 | apple |
    | 2 | orange |
    | 3 | pear |
    +----+----------+
    3 rows in set (0.00 sec)

    Using <pre> tags won't work, due to other complications with the way
    carriage returns are being automatically converted to <br> tags at the
    end of each line.

    Can anyone recommend a PHP preg_replace statement that would change all
    spaces within <code></code> tags to &nbsp; nonbreaking spaces? (This
    seems like it ought to be out there somewhere in dozens of examples; if
    it is, please feel free to rub my nose in my failure to RTFM.)

    I really want to do this with a regular expression and a bunch of other
    crawling around; each data record is already filtered through
    preg_replace to pop external links up in new browser windows and so
    forth. It would be outstanding if I could just add another find pattern
    and another replace pattern to the existing module and be done.

    Thanks very much,

    --Kent
  • Doug Hutcheson

    #2
    Re: How to change text inside an HTML tag?

    "Kent Brewster" <kent@mindsack. com> wrote in message
    news:5K5vc.3381 $7g5.1115@newss vr27.news.prodi gy.com...[color=blue]
    > I've got a (large, hairy, evil) database full of raw dumps from MySQL
    > that look like this:
    >
    > <code>
    > +----+----------+
    > | k | t |
    > +----+----------+
    > | 1 | apple |
    > | 2 | orange |
    > | 3 | pear |
    > +----+----------+
    > 3 rows in set (0.00 sec)
    > </code>
    >
    > When displayed in HTML, the extra spaces don't show, leaving this:
    >
    > +----+----------+
    > | k | t |
    > +----+----------+
    > | 1 | apple |
    > | 2 | orange |
    > | 3 | pear |
    > +----+----------+
    > 3 rows in set (0.00 sec)
    >
    > Using <pre> tags won't work, due to other complications with the way
    > carriage returns are being automatically converted to <br> tags at the
    > end of each line.
    >
    > Can anyone recommend a PHP preg_replace statement that would change all
    > spaces within <code></code> tags to &nbsp; nonbreaking spaces? (This
    > seems like it ought to be out there somewhere in dozens of examples; if
    > it is, please feel free to rub my nose in my failure to RTFM.)
    >
    > I really want to do this with a regular expression and a bunch of other
    > crawling around; each data record is already filtered through
    > preg_replace to pop external links up in new browser windows and so
    > forth. It would be outstanding if I could just add another find pattern
    > and another replace pattern to the existing module and be done.
    >
    > Thanks very much,
    >
    > --Kent[/color]

    Kent,
    Presumably there would be fewer replacements - thus fewer CPU cycles -
    involved in either preventing the conversion of newline to <br> for those
    fragments, or post-processing the fragment to revert <br> to newline?
    Just my $0.02.
    Doug

    --
    Remove the blots from my address to reply


    Comment

    • Larry Jaques

      #3
      Re: How to change text inside an HTML tag?

      On Tue, 01 Jun 2004 20:33:05 GMT, Kent Brewster <kent@mindsack. com>
      stated wide-eyed, with arms akimbo:
      [color=blue]
      >I've got a (large, hairy, evil) database full of raw dumps from MySQL
      >that look like this:
      >
      ><code>
      >+----+----------+
      >| k | t |
      >+----+----------+
      >| 1 | apple |
      >| 2 | orange |
      >| 3 | pear |
      >+----+----------+
      >3 rows in set (0.00 sec)
      ></code>
      >
      >When displayed in HTML, the extra spaces don't show, leaving this:
      >
      >+----+----------+
      >| k | t |
      >+----+----------+
      >| 1 | apple |
      >| 2 | orange |
      >| 3 | pear |
      >+----+----------+
      >3 rows in set (0.00 sec)
      >
      >Using <pre> tags won't work, due to other complications with the way
      >carriage returns are being automatically converted to <br> tags at the
      >end of each line.[/color]

      Does your database contain discrete data for each column, or are
      these text dumps within a database field you're extracting?

      If text dumps, g'luck.

      If it's discrete (k contains values 1, 2, 3; t contains values apple,
      orange, pear, etc.), why not use a table and CSS text classes to make
      set-pixel-width boxes, then drop the data into them with a "while"
      structure?

      Can you post a sample data set?


      --
      STOP LIVING LIKE VEAL
      -----------------------
      http://diversify.com Veal-free Websites

      Comment

      • Andy Hassall

        #4
        Re: How to change text inside an HTML tag?

        On Tue, 01 Jun 2004 20:33:05 GMT, Kent Brewster <kent@mindsack. com> wrote:
        [color=blue]
        >I've got a (large, hairy, evil) database full of raw dumps from MySQL
        >that look like this:
        >
        ><code>
        >+----+----------+
        >| k | t |
        >+----+----------+
        >| 1 | apple |
        >| 2 | orange |
        >| 3 | pear |
        >+----+----------+
        >3 rows in set (0.00 sec)
        ></code>
        >
        >When displayed in HTML, the extra spaces don't show, leaving this:
        >
        >+----+----------+
        >| k | t |
        >+----+----------+
        >| 1 | apple |
        >| 2 | orange |
        >| 3 | pear |
        >+----+----------+
        >3 rows in set (0.00 sec)[/color]

        OK, that's expected.
        [color=blue]
        >Using <pre> tags won't work, due to other complications with the way
        >carriage returns are being automatically converted to <br> tags at the
        >end of each line.
        >
        >Can anyone recommend a PHP preg_replace statement that would change all
        >spaces within <code></code> tags to &nbsp; nonbreaking spaces? (This
        >seems like it ought to be out there somewhere in dozens of examples; if
        >it is, please feel free to rub my nose in my failure to RTFM.)
        >
        >I really want to do this with a regular expression and a bunch of other
        >crawling around; each data record is already filtered through
        >preg_replace to pop external links up in new browser windows and so
        >forth. It would be outstanding if I could just add another find pattern
        >and another replace pattern to the existing module and be done.[/color]

        If you've already got newlines sussed, then basically all you want to do is
        change any sequence of n spaces (n>1) into a space and (n-1) &nbsp; entities
        (if you want to allow longish lines to wrap. If not, then ditch the leading
        space and just output n &nbsp;s). Something like (untested):

        preg_replace_ca llback('/( {2,})/',
        create_function ('$m',
        'return " ".str_repeat("& nbsp;",strlen($ matches[1])-1);'));

        If you've got tabs in there, you'll probably want a prior step turning them
        into 4 or 8 spaces depending on your preference. Then present the lot in a
        monospace font.

        Since you only want it between <code> tags, you probably want that function
        called as a callback from another preg_replace_ca llback('/<code>(.*?)<\/code>',
        ....). At which point you're probably better of ditching the create_function 's
        and just making them ordinary functions (I tend to overuse create_function as I
        think in Perl for this sort of thing, where anonymous subs have much cleaner
        syntax).

        So something vaguely like:

        function munch_spaces($m atches)
        {
        return ' '.str_repeat('& nbsp;', strlen($matches[1]) - 1);
        }

        function crunch_code($ma tches)
        {
        return preg_replace_ca llback(
        '/( {2,})/',
        'munch_spaces',
        $matches[1]
        );
        }

        preg_replace_ca llback(
        '/<code>(.*?)<\/code>/',
        'crunch_code',
        $large_hairy_ev il
        );

        --
        Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
        http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

        Comment

        Working...