All pastes #780248 Raw Edit

Something

public text v1 · immutable
·published 2007-11-18 15:14 UTC
rendered paste body
<?php
/**
 * Source for the lite_parser, the parser for lite personal pages.
 *
 * @package RSHiLib_Parser
 * @copyright Copyright 2007 © Ville Kalliomäki (Rithiur)
 * @author Ville Kalliomäki (Rithiur) <rithiur@gmail.com>
 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
 */

/**
 * Parser for highscores lite pages.
 * This parser parses the data out of page containing all the skill data for
 * one person. The data is returned as an array where each index is the colum
 * in the page in lower case (either 'skill', 'level', 'rank' or 'xp'). And each
 * value contains array containing the data for skills. Note that skills which
 * are not ranked for the person are not contained within the array.
 *
 * @package RSHiLib_Parser
 */
class lite_parser extends page_parser
{

	/**
	 * Parses skill data out of the lite pages.
	 * @param string Page to parse for data
	 * @return array Returns array of skill data, or false on failure
	 */
	public function parse ($page)
	{
		global $rshl_skills;

		// Generate empty array
		$char = array(
			'skill' => array(),
			'rank' => array(),
			'level' => array(),
			'xp' => array(),
		);

		// Return empty array, if we got the page no found (i.e. not available)
		if (strpos($page, '404 - Page not found') !== false)
		{
			return $char;
		}

		$count = preg_match_all('/(-?\d+),(-?\d+),(-?\d+)/', $page, $match);

		if (!$count)
		{
			trigger_error('Invalid highscore page', E_USER_WARNING);
			return false;
		}

		// Make sure the number of skills in highscores and on us matches
		if ($count != count($rshl_skills))
		{
			trigger_error('Invalid number of skills', E_USER_WARNING);
			return false;
		}

		$skills = array_values($rshl_skills);

		// Add existing skills to the return value
		for ($i = 0; $i < $count; $i++)
		{
			if (intval($match[1][$i]) > 0)
			{
				$char['skill'][] = $skills[$i];
				$char['rank'][] = $match[1][$i];
				$char['level'][] = $match[2][$i];
				$char['xp'][] = $match[3][$i];
			}
		}

		return $this->make_int($char);
	}

}

?>