Aaron: m
#
# - 1890: 147
# - 1900: 193
# ...
#
function print_html($line) {
$tokens = explode(' ', $line);
?>
= $tokens[0] ?>: = $tokens[1] ?>
- = $year ?>: = $tokens[$i] ?>
# 147
# 193
# ...
#
function print_xml($line) {
$tokens = explode(' ', $line);
$xmlDoc = new DOMDocument();
$baby_tag = $xmlDoc->createElement('baby');
$baby_tag->setAttribute('name', $tokens[0]);
$baby_tag->setAttribute('gender', $tokens[1]);
$year = 1890;
for ($i = 2; $i < count($tokens); $i++) {
$rank_tag = $xmlDoc->createElement('rank');
$rank_tag->setAttribute('year', $year);
$rank = $xmlDoc->createTextNode($tokens[$i]);
$rank_tag->appendChild($rank);
$baby_tag->appendChild($rank_tag);
$year += 10;
}
$xmlDoc->appendChild($baby_tag);
print $xmlDoc->saveXml();
}
# Prints out a JSON representation of the given line of data
# for the data, "Aaron m 147 193 187 199 250 237 230 178 52 34 34 41 55",
# would produce the following JSON:
# {
# 'name': 'Aaron',
# 'gender': 'm',
# 'ranks': [
# {'year': 1890, 'rank': '147'},
# {'year': 1900, 'rank': '193'},
# ...
# ]
# }
function print_json($line) {
$tokens = explode(' ', $line);
$json = array(
'name' => $tokens[0],
'gender' => $tokens[1],
'rankings' => array()
);
for ($i = 2; $i < count($tokens); $i++) {
array_push($json['rankings'], $tokens[$i]);
}
print json_encode($json);
}
function get_query_param($name) {
if (!isset($_GET[$name])) {
header("HTTP/1.1 400 Invalid Request - Missing parameter");
die("HTTP/1.1 400 Invalid Request: missing required parameter '$name'");
}
if ($_GET[$name] == "") {
header("HTTP/1.1 400 Invalid Request - Non-empty parameters required");
die("HTTP/1.1 400 Invalid Request: parameter '$name' must be non-empty");
}
return $_GET[$name];
}
?>