在 PHP 中将 URL 变量名缩写替换为全名

发布于 2024-11-07 10:48:33 字数 3075 浏览 0 评论 0原文

我将一个 URL 变量传递到一个 PHP 页面,该页面输出我们的田径成绩的 XML 提要。

例如,我们的男子篮球 XML feed 的 URL 变量是“m-baskbl”。其他运动项目也有类似的缩写。

问题是我需要能够输出完整的运动名称,即“Men's篮球”,但该字符串没有出现在 XML 中。

我的问题是,你知道在 PHP 中用运动名称替换 URL 变量缩写的方法吗?例如,让“m-baskbl”输出“男子篮球”。

这是我的页面XML 和代码:

        <?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰魂雪魄 2024-11-14 10:48:33

您也许可以做一个简单的数组映射,例如

$Abbreviations = array(
   'm-baskbl' => "Men's Basketball",
   'w-baskbl' => "Women's Basketball"
);

// somewhere you know you're fetching data for "m-baskbl"
// use that variable to fetch the actual name
$Division = $Abbreviations['m-baskbl']; // Men's Basketball

只是一个想法:)

You can probably do a simple array mapping e.g.

$Abbreviations = array(
   'm-baskbl' => "Men's Basketball",
   'w-baskbl' => "Women's Basketball"
);

// somewhere you know you're fetching data for "m-baskbl"
// use that variable to fetch the actual name
$Division = $Abbreviations['m-baskbl']; // Men's Basketball

Just an idea :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文