in reply to Converting HTML tags into uppercase using Perl

Hi steve_g50, Try this,

#!/usr/bin/perl -w use strict; local $/; open(INPUT, "input.html") || die("$!"); open(OUTPUT, ">output.html") || die ("$!"); my $txt = <INPUT>; $txt=~s/<([^> ]*)([^>]*>)/"<".uc($1)."$2"/egsi; #Updated: only element + names except attributes print OUTPUT $txt;

Regards,
Velusamy R.


eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Replies are listed 'Best First'.
Re^2: Converting HTML tags into uppercase using Perl
by Corion (Patriarch) on Nov 29, 2005 at 11:09 UTC

    This script of course breaks for the case of an embedded ">" sign in the value, and it uppercases all values too, both of which will break the HTML file:

    <html> <img src="a_greater_b.gif" alt="a > b" /> <img src="a_smaller_b.gif" alt="a < b"/> </html>

    These niggles are the reason why it is always recommended to avoid parsing HTML with regular expressions.

    Update: Rearranged HTML to be a test case for the second problem as well.

Re^2: Converting HTML tags into uppercase using Perl
by steve_g50 (Initiate) on Nov 29, 2005 at 13:16 UTC
    Thanks Samy_rio, i'll see what i can do with your program. Thank you to everyone else for your help. Keep replying if you can still help with my problem. Merry Christmas to all of you who celebrate it.
Re^2: Converting HTML tags into uppercase using Perl
by steve_g50 (Initiate) on Nov 29, 2005 at 11:53 UTC
    This works very well, thanks. But how do i get the program to ASK for a .htm or .html file, then change the tags in the given file to uppercase, and THEN save the new file using the OPEN fuction? Thanks again.

      It doesn't work very well for all of the reasons thar Corion listed. It will break badly on various (common) types of HTML. Please look at using a solution that uses a real HTML parser.

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg