cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

Re: php and pdf download

From: Dave Withnall <withnall-at-inspired.net.au_at_lysander.inspired.net.au>
Date: Thu, 20 Nov 2003 18:21:04 +1000

TQatSJU_at_aol.com wrote:

>does anyone know of a php class, script etc... that extracts form fields like
>daniel's formfind.pl script? If not, i'm going to start working on one, and
>any interested parties who would like to help can email me....
>
>thanks, Tom
>
>
>
>
I've stuck in a php class I did up for a project I was working on for
work at the end of this email.
It's a heavily plagarised conversion of the perl form find utility that
Daniel Sternberg setup.

Basically you pass in a file name and it'll run through it looking for
HTML form tags and store them in an internal array.

It handles images, submit buttons, radio tags, text areas and the other
normal fields. It'll tell you what it is, their name, if they're
selected (radio tags) or what the contents are (everything else)

I just nuked a few bits that related specifically to that project, but
there might be a few more bits in there that don't make sense or aren't
used.

Play with it as you desire.

<?php

class htmlparser {
    var $tagarray;
    var $datafilename;
   
    
/************************************************************************************
    * Constructor
    * @return Array Pointer
    * @desc Parse a string for HTML Tag identifiers (Name, Value & Type).
    
************************************************************************************/
    function htmlparser($datafile) {
        $this->setDataFilename($datafile);
        $this->parseHTML();
    }
   
    /**
    * @return Array Pointer
    * @desc Parse a string for HTML Tag identifiers (Name, Value & Type).
    */
    function &namevalue($str) {
        // get name tag
        if (preg_match("/name *=/i", $str)) {
            preg_match("/.*name *= *(\"|\'|)([^ (\"|\')>]*).*/i", $str,
$name);
            $name = $name[2];
        } else {
            $name=""; // no name given
        }
       
        // get value tag
        if (preg_match("/value *=/i", $str)) {
            preg_match("/.*value *= *(\"|\'|)([^(\"|\')>]*).*/i", $str,
$value);
            $value = $value[2];
        } else {
            $value="";
        }
       
        // get type tag
        if (preg_match("/type *=/i", $str)) {
            preg_match("/.*type *= *(\"|\'|)([^ (\"|\')>]*).*/i", $str,
$type);
            $type = $type[2];
        } else {
            $type="";
        }
       
        switch($type) {
            case "submit":
            case "image":
                break;
            case "radio":
                if (preg_match("/checked/i", $str)) {
                    $this->tagarray[$name]["value"] = $value;
                    $this->tagarray[$name]["type"] = $type;
                }
                break;
            default:
                $this->tagarray[$name]["value"] = $value;
                $this->tagarray[$name]["type"] = $type;
                break;
        } // switch
   
        return $name;
    }
   
    /**
    * @return void
    * @desc Parse the currrent Data File for the Session & Page ID tags.
    */
    function parseHTML() {
        $fp = fopen ($this->getDataFilename(), "r"); // open up
the file downloaded
       
        while (!feof ($fp)) {
            $str = fgets ($fp);
           
            preg_match_all("/[^<]*(<[^>]+>)/", $str, $match);
            $tempstr = $str;

            foreach ($match[1] as $str) {
                if (preg_match("/^<!--/", $str)) {
                    // this is a comment tag, ignore it
                } else {
                    if (preg_match("/^< *(input|select|textarea)/i",
$str, $tag)) {
                        $value = "";
                        $name = "";
                        $type = "";
                        if (strcasecmp($tag[1], "textarea") == 0) {
                            $name = $this->namevalue($str);
                            $value = substr($tempstr, strpos($tempstr,
$str) + strlen($str));
                           
                            while(strpos($value, "</textarea>") === false) {
                                $value .= fgets($fp);
                            } // while

                            $pos = strpos($value, "</textarea>");
                            $value = substr($value, 0, $pos);
                           
                            $str = "<select type=\"textarea\" name=\"" .
$name . "\" value=\"" . $value . "\">";
                        } // end if text area
                       
                        if (strcasecmp($tag[1], "select") == 0) {
                            $select = true;
                            $name = $this->namevalue($str);
                           
                            while($select) {
                                $str = fgets ($fp);
                               
                                if (preg_match("/< *\/ *select *>/i",
$str, $tag)) { // Come to end of select group with nothing being selected.
                                    $select = false;
                                }
                               
                                if (preg_match("/^< *option.*value *=
*(\"|\'|)([^ (\"|\')>]*).*selected>.*/i", $str, $tag)) {
                                    $value = $tag[2];
                                    $select = false;
                                }
                            } // while

                          $str = "<select type=\"select\" name=\"" .
$name . "\" value=\"" . $value . "\">";
                        } // end if select
                       
                        $this->namevalue($str);
                    }
                }
            }
        }
       
        fclose ($fp);
    }
   
    
/************************************************************************************
    * Mutators
    
************************************************************************************/

    /**
    * @return void
    * @param value string
    * @desc set true/false if an error has occured.
    */
    function setDataFilename ($value) {
        $this->datafilename = $value;
    }
   
    
/************************************************************************************
    * Accessors
    
************************************************************************************/

    /**
    * @return string
    * @desc get info of an error occuring
    */
    function getDataFilename () {
        return $this->datafilename;
    }
   
    /**
    * @return Array
    * @desc Get the tags found on the page.
    */
    function getTagArray() {
        return $this->tagarray;
    }
}
?>

-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive? Does it
help you create better code? SHARE THE LOVE, and help us help
YOU! Click Here: http://sourceforge.net/donate/
Received on 2003-11-20