Posted by Rithiur Thu 1st Mar 2007 17:32 - Syntax is PHP - 15 viewsRun this post in the PHP shell
Download | New Post | Modify | Hide line numbers
PHP parser reported no syntax errors in this post!
Description:
Class for parsing RuneScape highscore data.

  1. /**
  2. * Source for the single_parser, the parser for single personal pages
  3. *
  4. * @package RSHiLib_Parser
  5. * @copyright Copyright 2007 © Ville Kalliomäki (Rithiur)
  6. * @author Ville Kalliomäki (Rithiur) <>
  7. * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  8. */
  9.  
  10. /**
  11. * Parser for personal stat pages.
  12. * This parser parses the data out of page containing all the skill data for
  13. * one person. The data is returned as an array where each index is the colum
  14. * in the page om öpwer case (either 'skill', 'level', 'rank' or 'xp'). And each
  15. * value contains array containing the data for skills. Note that skills which
  16. * are not ranked for the person are not contained within the array.
  17. *
  18. * @package RSHiLib_Parser
  19. */
  20. class single_parser
  21. {
  22.  
  23.     /**
  24.      * Parses skill data out of the personal pages.
  25.      * @param string Page to parse for data
  26.      * @return array Returns array of skill data, or false on failure
  27.      */
  28.     function parse ($page)
  29.     {
  30.         // Match the important table out
  31.         if (
  32.             !preg_match('#.*]*>(.*?personal.*?)
    #si'
    , $page, $match) ||
  33.             !preg_match_all('#]*>(.*)#Usi', $match[1], $rows)
  34.         )
  35.         {
  36.             return false;
  37.         }
  38.  
  39.         $rows[1] = str_replace(' ', '', $rows[1]);
  40.         $table = array();
  41.  
  42.         // Parse out the skill data
  43.         foreach ($rows[1] as $row)
  44.         {
  45.             preg_match_all('#]*>(.*)#Usi', $row, $cols);
  46.  
  47.             $cols[1] = array_map('trim', array_map('strip_tags', $cols[1]));
  48.             $cols[1] = array_values(preg_grep('/./', $cols[1]));
  49.  
  50.             // Don't do this for too small rows
  51.             if (count($cols[1]) < 4)
  52.             {
  53.                 continue;
  54.             }
  55.  
  56.             // Set keys, if they don't exist yet
  57.             if (!isset($keys))
  58.             {
  59.                 $keys = array_map('strtolower', $cols[1]);
  60.  
  61.                 foreach ($keys as $key)
  62.                 {
  63.                     $table[$key] = array();
  64.                 }
  65.             }
  66.  
  67.             // Get each important cells out
  68.             else
  69.             {
  70.                 foreach ($keys as $cell => $key)
  71.                 {
  72.                     $table[$key][] = $cols[1][$cell];
  73.                 }
  74.             }
  75.         }
  76.  
  77.         // Turn integer data into integers
  78.         foreach (array('rank', 'xp', 'level') as $col)
  79.         {
  80.             $table[$col] = array_map('intval', str_replace(',', '', $table[$col]));
  81.         }
  82.  
  83.         return $table;
  84.     }
  85.  
  86. }
  87.  
  88. ?>

PermaLink to this entry https://pastebin.co.uk/11241
Posted by Rithiur Thu 1st Mar 2007 17:32 - Syntax is PHP - 15 viewsRun this post in the PHP shell
Download | New Post | Modify | Hide line numbers

 

Comments: 0