in reply to Perl HTML Form elements Filter

Everyone else is quite right that the sensible person uses a module for this. However, if you aren't sensible, or if you're writing for people who can't necessarily install modules on their machine, you might do something like this:

First split the HTML into chunks, and don't process bits of HTML outside < and >. Again, the wise person would use a module for this, but the rash fool would just split on ">". Then, for each chunk:

if (/name\s*=\s*['"]?(\w+)/i) { $name = $1; }

The only interesting thing here is the ['"]? which makes sure that you don't grab any quotes by accident. As far as I recall, form names should only have word characters, but I am ready to be corrected. I also don't know whether the spaces between html attribute names and values are legit or not. But unless you are writing the html yourself, you can trust some foolish user to put them in.

If you just want _form_ element names, you'll also first need to test for what sort of html tag you have:

if (/<input|<select|<textarea/i)

Cheers Dave

Replies are listed 'Best First'.
Re: Perl HTML Form elements Filter
by skazat (Chaplain) on Dec 01, 2000 at 04:53 UTC

    here, I gots it:

    while(<>) { while (/(input|textarea|select).*?name="?(.*?)"?\s+/gsi) { print " +$2\n" } }

    thats from markjugg.

     

    -justin simoni
    !skazat!

      I'm not sure what I was thinking when I put the while loop instead the while loop. I like it better like this:
      while(<>) { if (/(input|textarea|select).*?name="?(.*?)"?[\s>]/gsi) { prin +t "$2\n" } }
      update: This has the known bug of failing if the tag spans multiple lines. Maybe some other ones, too. :)

      -mark