在 PHP 中将 URL 变量名缩写替换为全名
我将一个 URL 变量传递到一个 PHP 页面,该页面输出我们的田径成绩的 XML 提要。
例如,我们的男子篮球 XML feed 的 URL 变量是“m-baskbl”。其他运动项目也有类似的缩写。
问题是我需要能够输出完整的运动名称,即“Men's篮球”,但该字符串没有出现在 XML 中。
我的问题是,你知道在 PHP 中用运动名称替换 URL 变量缩写的方法吗?例如,让“m-baskbl”输出“男子篮球”。
<?php
// Output the current school year (previous year through July 31, current year on/after August 1)
$year = date('Y') - 1;
$month = date('n');
if($month >= 8) $year++;
// Build the XML file path
$p ="http://www.gostanford.com/data/xml/events/";
$s = $_GET['s'];
$e = "/$year/index.xml";
$file = "$p$s$e";
$xml = simplexml_load_file($file);
// Reverse chronological order
$xmlArray = array();
foreach ($xml as $event_date) $xmlArray[] = $event_date;
$xmlArray = array_reverse($xmlArray);
foreach ($xmlArray as $event_date)
{
// Only output if complete
if(!empty($event_date->event['vn']) && !empty($event_date->event['hn']) && !empty($event_date->event['vs']) && !empty($event_date->event['hs']))
{
// Is Stanford the home team?
$home = ($event_date->event['hc'] == 'stan');
// Only show opponents' name
$name = $home ? $event_date->event['vn'] : $event_date->event['hn'];
// Output "vs" if home game or "at" if away game
$preposition = $home ? 'vs' : 'at';
// Tie?
if((int)$event_date->event['hs'] == (int)$event_date->event['vs'])
{
$result = 'Tie'; // Tie
}
else if((int)$event_date->event['hs'] > (int)$event_date->event['vs']) // Home team won?
{
if($home) // Home?
{
$result = 'Win'; // You're home and the home team won
}
else
{
$result = 'Loss'; // You're away, but the home team won
}
}
else // Home team lost
{
if($home) // Home?
{
$result = 'Loss'; // You're away, but the home team won
}
else
{
$result = 'Win'; // You're home and the home team won
}
}
echo '<li>';
echo '<h3>', $preposition, ' ', $name, ' - ', '<em>', $result, '</em></h3>';
echo '<p><strong>', $event_date->event['vn'], ' ', $event_date->event['vs'], ' - ', $event_date->event['hn'], ' ', $event_date->event['hs'], '</strong></p>';
echo '<p>', date('F j, Y', strtotime($event_date['date'])), '</p>';
echo '</li>';
}
}
?>
I'm passing a URL variable to a PHP page that is outputting an XML feed of our athletics' scores.
For example, the URL variable for our men's basketball XML feed is "m-baskbl". Other sports have similar abbreviations.
The problem is I need to be able to output the full sport name, i.e. "Men's Basketball", but this string doesn't appear in the XML.
My question is, do you know of a way to replace the URL variable abbreviation with the sport name in PHP? For example, have "m-baskbl" output "Men's Basketball".
Here is my page, XML and code:
<?php
// Output the current school year (previous year through July 31, current year on/after August 1)
$year = date('Y') - 1;
$month = date('n');
if($month >= 8) $year++;
// Build the XML file path
$p ="http://www.gostanford.com/data/xml/events/";
$s = $_GET['s'];
$e = "/$year/index.xml";
$file = "$p$s$e";
$xml = simplexml_load_file($file);
// Reverse chronological order
$xmlArray = array();
foreach ($xml as $event_date) $xmlArray[] = $event_date;
$xmlArray = array_reverse($xmlArray);
foreach ($xmlArray as $event_date)
{
// Only output if complete
if(!empty($event_date->event['vn']) && !empty($event_date->event['hn']) && !empty($event_date->event['vs']) && !empty($event_date->event['hs']))
{
// Is Stanford the home team?
$home = ($event_date->event['hc'] == 'stan');
// Only show opponents' name
$name = $home ? $event_date->event['vn'] : $event_date->event['hn'];
// Output "vs" if home game or "at" if away game
$preposition = $home ? 'vs' : 'at';
// Tie?
if((int)$event_date->event['hs'] == (int)$event_date->event['vs'])
{
$result = 'Tie'; // Tie
}
else if((int)$event_date->event['hs'] > (int)$event_date->event['vs']) // Home team won?
{
if($home) // Home?
{
$result = 'Win'; // You're home and the home team won
}
else
{
$result = 'Loss'; // You're away, but the home team won
}
}
else // Home team lost
{
if($home) // Home?
{
$result = 'Loss'; // You're away, but the home team won
}
else
{
$result = 'Win'; // You're home and the home team won
}
}
echo '<li>';
echo '<h3>', $preposition, ' ', $name, ' - ', '<em>', $result, '</em></h3>';
echo '<p><strong>', $event_date->event['vn'], ' ', $event_date->event['vs'], ' - ', $event_date->event['hn'], ' ', $event_date->event['hs'], '</strong></p>';
echo '<p>', date('F j, Y', strtotime($event_date['date'])), '</p>';
echo '</li>';
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您也许可以做一个简单的数组映射,例如
只是一个想法:)
You can probably do a simple array mapping e.g.
Just an idea :)