Menu

Anual Stats Parsing Function

Billy
2006-08-16
2013-04-16
  • Billy

    Billy - 2006-08-16

    All,

    In my endevour to add salary cap functionallity to OFFL, I created this function to pull a players stats from the nfl.com player page and return an array with the associated value.

    syntax is:
    <array> = statsParse(<nfl_player_page>, <year>);

    Where <nfl_player_page> is the player page associated with that player in the OFFL players table.

    You must specify <year> as this function will only process a single season at a time. This was to keep the data structure simple.

    ------------------------------------------
    Limitations:
    Currently the year and team is not parsed as I didn't need that data. It would be easy to change if you want it. Currently I am only parsing the first 10 values in a table as, again that was all of the data I needed.

    ---------------------------------------------
    To use use it: I will reply to this message with a cut and paste of the code. simply create a file in OFFL_ROOT/lib directory name it somthing along the lines of custom_functions.php or MyFunctions.php ..... Use your favorite editor to paste the code into the file.

    Add this to the top of your php page where you want to use the function:

    require_once($DOC_ROOT . "lib/<your-file>.php");

    -----------------------------------------
    This function returns an array with the elements for each stat type. A "print_r($array)" will look somthing like this.

    array ( recieving => (array  0=>#num
                                 1=>#num
                                 2=>#num)
            passing => (array    0=>#num
                                 1=>#num)

    It works for everything but defense. There is room for improvement as I did get lazy :)

     
    • Billy

      Billy - 2006-08-16

      function statsParse($player_page, $year)
      {
              $data_raw = file($player_page);
                      // Eliminate blank lines
                      foreach ($data_raw as $line => $tag) { ;
                        if ($tag != '') { ;
                          $data = $data . $tag ;
                              }
                      }
              $data = explode('<div class="SLTables1">', $data);
              $count = count($data);
              $data[0] = "Removed Data"; // Data not needed
              $i = 1;
              // Determine what stats are available for a player
              // and what array element contains the data for a
              // given data type. NFL.com only publishes stats tables
              // if a player has stats available, without knowing the
              // search is necissary. The <$i> value is the array
              // element with the table heading. The next array
              // element actually contains the html table.
              while ($i <= $count) {;
              $index = $data[$i];
              if (preg_match('/PASSING/', $index)) { ;
              $table[passing] = $i;
              }
              if (preg_match('/RUSHING/', $index)) { ;
              $table[rushing] = $i;
              }
              if (preg_match('/RECEIVING/', $index)) { ;
              $table[recieving] = $i;
              }
              if (preg_match('/FUMBLES/', $index)) { ;
              $table[fumbles] = $i;
              }
              if (preg_match('/KICK RETURNS/', $index)) { ;
              $table[kick_ret] = $i;
              }
              if (preg_match('/KICKING/', $index)) { ;
              $table[kicking] = $i;
              }
              if (preg_match('/PUNT RETURNS/', $index)) { ;
              $table[punt_ret] = $i;
              }
              $i = $i + 2;
              }
             // Begin to chop up the data
              foreach ($table as $key=>$i) {;
              $i = $i + 1;
              $raw_data = $data[$i];
              $raw_data = explode("$year", $raw_data);
              $raw_data = $raw_data[1];
              $raw_data = explode("<tr class=\&quot;bg2\&quot;>", $raw_data);
              $raw_data = $raw_data[0];
              $raw_data = explode("<td>", $raw_data);
              // Set the type to float to remove all non numeric chars

              settype($raw_data[2], "float");
              settype($raw_data[3], "float");
              settype($raw_data[4], "float");
              settype($raw_data[5], "float");
              settype($raw_data[6], "float");
              settype($raw_data[7], "float");
              settype($raw_data[8], "float");
              settype($raw_data[9], "float");
              settype($raw_data[10], "float");
              settype($raw_data[11], "float");

              $table[$key] = array(0=>$raw_data[2],1=>$raw_data[3],$raw_data[4],2=>$raw_data[5],3=>$raw_data[6],4=>$raw_data[7],5=>$raw_data[8],6=>$raw_data[9],7=>$raw_data[10],8=>$raw_data[11]);
              }
      return $table;
      }

       

Log in to post a comment.